How to clean up the Docker overlay2 directory safely
Codemia Knowledge Hub ·
To free space in /var/lib/docker/overlay2, start with Docker's prune commands (docker system prune, or the container, image, network and volume variants), which remove unused objects and their layers safely. Deleting directories inside overlay2 by hand breaks the images and containers that use them, so if pruning is not enough, reset the whole /var/lib/docker directory with Docker stopped.
What is in the overlay2 directory
Docker's overlay storage driver uses a union filesystem that presents several directories as one view, which keeps container filesystem layers efficient. Its parts are:
- Lowerdir: Holds the lower layers from the image.
- Upperdir: Holds the upper layers with writable changes.
- Workdir: Used by the overlay filesystem when changes are committed.
- Merged: The visible content presented to the user.
Over time, unused and dangling images, stale containers, orphaned volumes and unmanaged directories pile up here. Clean up when disk space is disappearing quickly or you see many dangling images and stale containers.
Step 1: prune unused Docker objects
Step 2: measure the overlay directory
The directory is typically /var/lib/docker/overlay2 and holds the layers of your images and containers:
Step 3: find what is using the space
If pruning did not free enough space, find the largest directories:
Do not delete those directories by hand. Docker keeps separate records of every layer under /var/lib/docker/image/overlay2, so removing a folder from overlay2 leaves images and containers pointing at data that no longer exists, and they fail to start or pull. Docker's own documentation warns against touching anything inside /var/lib/docker/. Instead, find out which object owns the space and remove it through Docker:
Step 4: rebuild the Docker data directory
When the storage is badly cluttered and pruning does not help, rebuild the environment. First back up the images and containers you need:
Restarting the Docker service is worth trying before anything drastic:
As a last resort, stop Docker and delete the whole data directory, not just overlay2, so the layer records go with the layers. This also deletes all volumes, so back up any volume data first. Then start Docker again and reload or rebuild your images:
Cleanup steps at a glance
| Action | Description |
| Prune Unused Objects | Use Docker prune commands to clean up unused objects. |
| Examine Overlay Storage | Inspect /var/lib/docker/overlay2 for bloat. |
| Identify Largest Directories | Use du to find the largest directory for analysis. |
| Find Space Owners | Map large directories to images, containers or build cache. |
| Advanced Cleanup and Rebuild | Backup and rebuild Docker environment if required. |
Related reading
- How to clear CrashLoopBackOff
- How to combine host network with the default network in docker-compose
- How to configure a Kubernetes Multi-Pod Deployment
- How to configure custom domain name for Amazon ECR
- How to configure docker-compose.yml for Kafka local development?
- How to connect ASP.Net Core to a SQL Server Docker container on Mac
- How to connect local kafka in docker container?
- How to connect locally hosted MySQL database with the docker container
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.