Docker
OverlayFS
Cleanup
Docker Directory
System Maintenance

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

bash
1# Remove stopped containers
2docker container prune
3
4# Remove unused images
5docker image prune
6
7# Remove unused networks
8docker network prune
9
10# Remove unused volumes
11docker volume prune
12
13# Remove all unused objects
14docker system prune

Step 2: measure the overlay directory

The directory is typically /var/lib/docker/overlay2 and holds the layers of your images and containers:

bash
# Check the size of the overlay directory
du -sh /var/lib/docker/overlay2

Step 3: find what is using the space

If pruning did not free enough space, find the largest directories:

bash
   du -ah /var/lib/docker/overlay2 | sort -rh | head -n 10

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:

bash
1   # Space used by images, containers, volumes and build cache
2   docker system df -v
3
4   # Which container writes to which overlay2 directory
5   docker ps -aq | xargs docker inspect --format '{{.Name}} {{.GraphDriver.Data.UpperDir}}'
6
7   # Unused images (not only dangling ones) and the build cache
8   docker image prune -a
9   docker builder prune

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:

bash
   docker save -o my_image.tar my_image
   docker export -o my_container.tar my_container_id

Restarting the Docker service is worth trying before anything drastic:

bash
   systemctl restart docker

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:

bash
1   systemctl stop docker
2   rm -rf /var/lib/docker
3   systemctl start docker
4   docker load -i my_image.tar
5   # or rebuild from the Dockerfile
6   docker build -t my_image .

Cleanup steps at a glance

ActionDescription
Prune Unused ObjectsUse Docker prune commands to clean up unused objects.
Examine Overlay StorageInspect /var/lib/docker/overlay2 for bloat.
Identify Largest DirectoriesUse du to find the largest directory for analysis.
Find Space OwnersMap large directories to images, containers or build cache.
Advanced Cleanup and RebuildBackup and rebuild Docker environment if required.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design