Skip to main content

Containers and Kubernetes · Beginner · 15 min

Clean up Docker disk usage safely

By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · Docker Engine 29.10

You will measure what Docker is storing, prune stopped containers, unused images, networks and build cache without touching your data, find a container whose writable layer has grown, schedule a weekly cleanup with a systemd timer and move Docker's storage to a bigger disk.

On this page

Docker removes almost nothing on its own. Apart from some automatic trimming of build cache, every image you pull, every container you stop, every build you run leaves something behind, and on a small server that adds up until the disk is full and the database next to it can no longer write. The fix is a set of prune commands, but they differ a lot in what they delete. Some remove nothing you would miss. One of them can remove your database.

In this tutorial you measure what Docker is holding, clear the safe categories one at a time, and learn which command to leave alone unless you have checked first. Then you find a container that is quietly writing files into itself, set up a weekly cleanup that never touches volumes, and move Docker's storage to a bigger disk when cleaning is not enough.

If the whole server is full and you do not yet know Docker is the cause, start with find what is using disk space on Linux and come back here once it points at /var/lib/docker or /var/lib/containerd.

Step 1 · See where Docker's space goes

Start with the disk itself, then ask Docker for its own breakdown:

Bash
df -h / /var/lib/docker /var/lib/containerd 2>/dev/null
docker system df

The docker system df table has one row each for images, containers, local volumes and build cache, with these columns:

  • TOTAL: how many of that thing exist.
  • ACTIVE: how many are in use by a container.
  • SIZE: the space they take.
  • RECLAIMABLE: what a prune could free, with a percentage.

For the detail behind each row, add -v:

Bash
docker system df -v

This prints a section per type. Under images, SHARED SIZE is space an image shares with other images (common base layers) and UNIQUE SIZE is space only that image uses. Removing an image frees roughly its unique size, not its full size. Under containers, the SIZE column is each container's own writable layer. Under volumes, LINKS is how many containers use each volume; 0 means none does right now.

Docker Engine 29 stores images in one of two places. Fresh installs of 29 use the containerd image store, which keeps image contents in /var/lib/containerd; servers upgraded from older versions usually still use the overlay2 driver under /var/lib/docker. Check which one you have, because it matters in the last step:

Bash
docker info -f '{{ .DriverStatus }}'

Check it worked

You can name the row with the largest RECLAIMABLE value, and you know whether the last command printed io.containerd.snapshotter.v1 (containerd image store) or something like [[Backing Filesystem extfs] [Supports d_type true] ...] (classic storage). On classic storage, docker info -f '{{ .Driver }}' prints overlay2. Write down the SIZE totals so you can compare them at the end.

Step 2 · Remove stopped containers and unused networks

Stopped containers keep their writable layer and their logs until you remove them. Look at them before you delete anything:

Bash
docker ps --all --filter status=exited --format 'table {{.Names}}\t{{.Image}}\t{{.Status}}'

If one of them is something you stopped on purpose and plan to start again, it will be removed with the rest, so start it or note how to recreate it first. A container created by Compose is recreated by docker compose up, so losing a stopped one costs nothing as long as its data sits in a volume.

Remove stopped containers that were created more than a day ago. This keeps containers you created in the last day, but a container created earlier and stopped just now is still removed, so check the list above first:

Bash
docker container prune --filter "until=24h"

Docker lists what it will do and asks you to confirm. Answer y. Then remove networks that no container uses:

Bash
docker network prune

Networks take almost no disk, but removing old ones keeps docker network ls readable and frees their address ranges.

Check it worked

Run docker ps --all --filter status=exited again. Only containers created in the last 24 hours remain. In docker system df, the containers row shows ACTIVE equal or close to TOTAL.

Step 3 · Remove images you no longer use

Docker has two kinds of unused image, and the default prune only touches the first:

  • Dangling images have no tag. They are usually old versions left behind when you pulled or built a newer image with the same tag. Nothing refers to them by name.
  • Unused images still have a tag, but no container, running or stopped, uses them.

Remove dangling images first. This is always safe:

Bash
docker image prune

Then look at what tagged images you have, and remove the ones no container uses that are older than a week:

Bash
docker image ls
docker image prune --all --filter "until=168h"

The until filter compares against when the image was built, not when you pulled it, so an image you pulled today for tomorrow's deploy can still be removed if it was built more than a week ago. Keep images you need by running a container from them, or skip this command and remove images by name.

Before you run the second command on a server with metered data, think about what you will pull again. On a prepaid mobile or capped connection, common across much of Africa and elsewhere, re-downloading a 1 GB image to roll back costs real money. Keep the previous version of each app image you might roll back to, and remove those by name with docker image rm <repository>:<tag> (replace with your own image) once you are sure.

Check it worked

docker system df shows a smaller SIZE on the images row, and docker image ls no longer lists any image with <none> as its tag.

Step 4 · Clear old build cache

If you build images on this server, BuildKit keeps cached layers and cache mounts so the next build is faster. On a build machine this is often the biggest row in docker system df. BuildKit trims some of this cache on its own once it passes a size limit, but that limit may be more than a small disk can spare, so it is worth clearing old cache yourself.

Remove cache that has not been used for three days:

Bash
docker builder prune --filter "until=72h"

Without a filter, docker builder prune removes dangling cache only. docker builder prune --all removes all unused cache, which is safe for your data but makes the next build slow, as it starts from nothing and downloads every base layer and dependency again.

Check it worked

The build cache row in docker system df is smaller. Your next build of an app still works; it may take longer for the steps whose cache you removed.

Step 5 · Treat volumes as data, not clutter

Read this before you run any volume command. A volume is where a database, an upload folder or an app's state lives. Removing a volume deletes that data for good. There is no undo and no recycle bin. A volume showing 0 links in docker system df -v only means no container uses it at this moment, which is also true of your database volume while you are upgrading its container.

List volumes that no container uses, and look inside any you do not recognise before removing it:

Bash
docker volume ls --filter dangling=true
docker volume inspect <volume-name>

Replace <volume-name> with a name from the list. The CreatedAt and Labels fields, and the name itself (Compose names volumes <project>_<volume>), usually tell you where it came from.

docker volume prune on its own removes only anonymous volumes: the ones with long random names that Docker creates when an image declares a volume and you did not name one. Those are usually leftovers. Run it, and read the list Docker prints before you confirm:

Bash
docker volume prune

Do not add --all unless you have checked every named volume in the list above. With --all, every unused named volume goes too, including the database volume of an app that happens to be stopped. To remove one named volume you have checked, name it directly with docker volume rm <volume-name>. For new volumes, you can add a label when you create them so a later prune can skip them:

Bash
docker volume create --label keep=true app-data
A label can only be set when a volume is created; you cannot add one to a volume that already exists, and your current database volume almost certainly has none. docker volume prune --all --filter "label!=keep" removes every unused named volume without the keep label. Only use it once every volume worth keeping was created with that label.

Back up any volume you are unsure about before removing it; keep container data with Docker volumes shows how with tar.

Check it worked

docker volume ls still lists every volume your apps need, and docker volume ls --filter dangling=true lists only volumes you have decided to keep.

Step 6 · Find a container whose writable layer has grown

A running container that writes files inside itself, instead of to a volume, grows without limit and prune cannot help, because the container is in use. Show every container with its writable layer size:

Bash
docker ps --all --size --format 'table {{.Names}}\t{{.Size}}\t{{.Status}}'

The SIZE column shows the writable layer first, then the virtual size (the image plus that layer) in brackets. A healthy container's writable layer is usually small, often kilobytes or a few megabytes. One reading in gigabytes is writing data where it should not.

To see which files it has added or changed, use docker container diff. A marks an added file, C a changed one and D a deleted one:

Bash
docker container diff <container-name> | head -50

Replace <container-name> with the one from the list. Typical causes are a log file the app writes to disk, a cache or temporary folder, or uploads saved inside the container. The fix is in how the container runs, not in pruning: mount a volume at that path, or set the app to log to standard output. Recreating the container resets its writable layer, but the growth comes back if the cause stays.

Container logs written to standard output are not part of the writable layer. Docker stores them next to it and, by default, never rotates them. If one container's logs are large, set log size limits as shown in install Docker Engine on Ubuntu 26.04; the limits apply to containers created after the change.

Check it worked

You can name the largest container's writable layer size and the folder it is writing to, from the docker container diff output. After you move that folder to a volume and recreate the container, its SIZE stays small a day later.

Step 7 · Schedule a weekly cleanup that never touches volumes

Pruning by hand works until you forget. A systemd timer runs the safe part every week. docker system prune removes stopped containers, unused networks, dangling images and unused build cache. Without --volumes it never deletes a volume, and this unit never passes it.

Create the service that does the work:

Bash
sudo tee /etc/systemd/system/docker-cleanup.service <<'EOF'
[Unit]
Description=Prune unused Docker data (never volumes)
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
ExecStart=/usr/bin/docker system prune --force --filter "until=168h" --filter "label!=keep"
EOF

--force skips the confirmation prompt, which a timer cannot answer. The until=168h filter leaves anything created in the last week, and label!=keep leaves containers and images you have labelled keep. If you want unused tagged images removed too, you can add --all after prune, but remember that until counts from when an image was built, not pulled: an image you pulled this week but that was built months ago would be removed, and re-downloading it costs data.

Create the timer that starts it every Sunday at about 03:00:

Bash
sudo tee /etc/systemd/system/docker-cleanup.timer <<'EOF'
[Unit]
Description=Weekly Docker cleanup

[Timer]
OnCalendar=Sun *-*-* 03:00:00
RandomizedDelaySec=30m
Persistent=true

[Install]
WantedBy=timers.target
EOF

Persistent=true runs a missed cleanup at the next boot if the server was off on Sunday night, and RandomizedDelaySec spreads the start over half an hour so a fleet of servers does not prune at once.

Load the units, enable the timer, and run the service once now to see it work:

Bash
sudo systemctl daemon-reload
sudo systemctl enable --now docker-cleanup.timer
sudo systemctl start docker-cleanup.service
journalctl -u docker-cleanup.service -n 20 --no-pager

For more on timers, calendar expressions and testing them, see replace cron jobs with systemd timers.

Check it worked

systemctl list-timers docker-cleanup.timer shows the next run on a Sunday. The journal ends with Docker's Total reclaimed space: line, and docker volume ls lists the same volumes as before the run.

Step 8 · Move Docker's storage to a bigger disk

When pruning frees too little, give Docker more room. This step assumes you have already added, formatted and mounted a new disk at /mnt/docker-disk (replace this with your own mount point) and that it is listed in /etc/fstab so it mounts at boot.

Read both warnings first. Stopping Docker stops every container on the server, so do this in a maintenance window. And the command below replaces /etc/docker/daemon.json: if that file exists, run cat /etc/docker/daemon.json and add the data-root key to it by hand instead, keeping your log settings.

Stop Docker and its socket, so nothing starts it again while you copy:

Bash
sudo systemctl stop docker.service docker.socket

Copy the data with rsync, keeping permissions, hard links, ACLs and extended attributes. The trailing slashes matter: they copy the contents of the folder, not the folder itself:

Bash
sudo mkdir -p /mnt/docker-disk/docker
sudo rsync -aHAX /var/lib/docker/ /mnt/docker-disk/docker/

Point Docker at the new location:

Bash
sudo tee /etc/docker/daemon.json <<'EOF'
{
  "data-root": "/mnt/docker-disk/docker"
}
EOF

If the first step showed io.containerd.snapshotter.v1, your images and container snapshots live in /var/lib/containerd, and data-root does not move them. Move them too while Docker is stopped:

Bash
sudo systemctl stop containerd.service
sudo mkdir -p /mnt/docker-disk/containerd
sudo rsync -aHAX /var/lib/containerd/ /mnt/docker-disk/containerd/
sudoedit /etc/containerd/config.toml

In the editor, add this line near the top of the file, above the first line that starts with [, then save:

TOML
root = "/mnt/docker-disk/containerd"

Start everything again:

Bash
sudo systemctl start containerd.service docker.service
docker info -f '{{ .DockerRootDir }}'
docker ps --all

Leave the old folders in place until your apps have run normally for a day. Then free the space on the old disk with sudo rm -rf /var/lib/docker (and /var/lib/containerd if you moved it). Check the path twice before you press Enter: that command cannot be undone.

Check it worked

docker info prints /mnt/docker-disk/docker, docker ps --all lists the same containers as before, your apps answer as usual, and df -h /mnt/docker-disk shows the space in use on the new disk.

Next steps

Compare docker system df now with the totals you wrote down in the first step; the difference is what you reclaimed. To stop containers writing into themselves, move their data into volumes with keep container data with Docker volumes. If a server is already full and refusing writes, fix a full disk on a Linux server covers the emergency steps, and find what is using disk space on Linux helps when the culprit is not Docker.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.