Docker cleanup means deleting the containers, images, volumes, networks and build cache nobody needs anymore, so your disk doesn’t fill up. Docker never cleans up on its own. Every build, every test container and every line of log output stays where it is until you remove it. That’s what the prune commands are for. They are fast and handy, but each one deletes something different, and a few of them behave differently from what most guides claim.
We measured it on 29 November 2026 inside an isolated throwaway Docker (Docker-in-Docker, Docker 29.10.1, containerd image store), so no command could touch the real server. Inside it we created a mess on purpose: three builds of the same app, stopped containers, an orphaned volume, a container logging like crazy. Then we ran every cleanup command on its own and checked what was left afterwards. Three results up front:
1.
docker image prune -adeleted two images and reported 97 kB reclaimed. The reason: their layers were shared with another image. “Image deleted” doesn’t mean “space freed”.2.
docker system prune -a --volumesleft our named volume alone. Since Docker 23,--volumesonly removes anonymous volumes. That protects databases with a named volume, but not those without one.3. A container that prints one line every millisecond produced 2.4 MB of log in 10 seconds. Extrapolated, that’s over 20 GB a day, and without a limit Docker just keeps writing.
By the way, on our own server docker system df showed 5.6 GB of build cache that morning, 5.5 GB of it reclaimable. Most servers that build images look like that. If your disk is already full and you don’t even know whether Docker is the culprit, start with Linux disk full. What containers and images are in the first place is covered in What is Docker?.
The video (two minutes, March 2026) shows compactly what each prune command removes. Below is what we found in addition when we measured it ourselves.
Docker cleanup: where the space goes
Before you delete anything, find out what is eating space. Docker spreads its storage across five buckets, and each has its own cleanup command:
| Bucket | What’s in it | Grows through | Clean up with |
|---|---|---|---|
| Images | Base images and the images you built | docker pull, docker build, updates | docker image prune |
| Containers | Each container’s writable layer, including stopped ones | Files the container writes itself | docker container prune |
| Volumes | Data meant to outlive a container | Databases, uploads, anonymous volumes | docker volume prune |
| Build cache | Intermediate steps of docker build | Every build, especially with changing dependencies | docker builder prune |
| Logs | Standard output of every container | Chatty applications | Set a log limit, not prune |
Networks take up practically no space, but docker system prune removes them too if no container uses them.
Important when you search with du: where the files live on disk depends on the storage backend. With the classic overlay2, almost everything sits under /var/lib/docker. With the newer containerd image store, which fresh Docker 29 installations use by default, images live under /var/lib/containerd. On our server that was 11 GB under /var/lib/docker and 22 GB under /var/lib/containerd. If you only know the first folder, you’re searching half the disk. This shows which backend you have:
docker info | grep -A1 "Storage Driver"
# Storage Driver: overlayfs
# driver-type: io.containerd.snapshotter.v1 ← containerd store

Step 1: Read docker system df properly
The most important Docker cleanup command doesn’t delete anything:
docker system df
This is what our test Docker looked like after building the app three times and starting a few containers:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 5 4 511.8MB 402.6kB (0%)
Containers 5 2 105MB 104.9MB (99%)
Local Volumes 3 2 31.46MB 31.46MB (99%)
Build Cache 15 0 394.7MB 209.8MB
The columns mean:
- TOTAL: how many objects exist.
- ACTIVE: how many of them a container uses. Careful: for images, a stopped container counts as a user too.
- SIZE: space on disk. Shared layers are counted once.
- RECLAIMABLE: what Docker thinks it can free if you clean up.
RECLAIMABLE is an estimate, not a promise. Before we started the containers, the images showed 259.9 MB (50%); afterwards 402 kB (0%), even though nothing about the images had changed. As soon as a container uses an image, it counts as not deletable, whether that container is running or has been stopped for weeks.
For details there’s the verbose view:
docker system df -v
It lists every image with SHARED SIZE and UNIQUE SIZE. The second column is the honest one: only that much is freed when exactly this image goes away. In our case node:22-alpine was 239 MB in size but had 402 kB unique size, because our app was built on top of it. That’s the reason for result number 1 above. -v also shows every container with its writable layer, every volume with its size and number of links (LINKS), and every build cache entry with its age.
Step 2: Remove stopped containers
docker container prune
This deletes all stopped containers. Running ones stay. In our test that was three, including one that had written 100 MB into its own writable layer. Result:
Deleted Containers:
ed3a4eccb7d5...
cd6fffc7c608...
b0fb0f3dad22...
Total reclaimed space: 104.9MB
Here the space actually freed matched what system df predicted. A container’s writable layer belongs to it alone; nothing is shared.
Two things you should know beforehand:
- A stopped container is sometimes intentional. If you halted a container with
docker stopto start it again later, you lose it. With Compose it doesn’t matter,docker compose up -drecreates it. With a hand-started container and a longdocker runcommand, it’s gone. - If a container writes a lot into its writable layer, that’s a design mistake. Data belongs in a volume. The writable layer disappears the next time the container is recreated anyway.
A time filter makes it more targeted. This command leaves everything younger than 24 hours alone:
docker container prune --filter "until=24h"
Step 3: Clean up images, and why “deleted” doesn’t mean “freed”
docker image prune # only "dangling" images without a name
docker image prune -a # all images no container uses
Dangling images are images without a name, shown as <none>:<none> in docker images. Classically they appear when you rebuild an image under the same tag: the old one loses its name and stays behind. With the classic store, docker image prune is therefore the standard command after builds.
Our first surprise: with the containerd image store we got no dangling images at all. We rebuilt myapp:latest five times with changed content, and docker images --filter dangling=true stayed empty every time. The space of the old versions didn’t vanish, it was just somewhere else, namely in the build cache (see step 4). If you’ve been running only docker image prune since moving to Docker 29, you’re no longer cleaning anything up, and you’ll be wondering why.
docker image prune -a goes further and deletes every image no container uses. Our test with a stopped nginx container:
Deleted Images:
untagged: node:22-alpine
untagged: alpine:3.22
deleted: sha256:49340b3467...
...
Total reclaimed space: 97.36kB
Three observations:
- The nginx image stayed, because the stopped container still used it. A stopped container holds on to its image.
node:22-alpinewas deleted, even though our app is built on it. That’s not a bug: the layers stay becausemyappneeds them; only the namenode:22-alpinedisappears. On the next build Docker downloads the base image again.- 97 kB instead of the expected 250 MB. Almost everything was shared. If you’re hunting for space, look at
UNIQUE SIZEindocker system df -v, not at the size indocker images.
-a is safe for your data, but not for your time: everything deleted has to be downloaded again on the next start or build. On a server with a slow line or a registry pull limit, that can hurt. Again, a filter helps:
docker image prune -a --filter "until=168h" # only images older than 7 days
How to keep images small in the first place, so there’s less to clean up, is covered in Reduce Docker image size. There a naive Node image shrank from 1.83 GB to 210 MB.
Step 4: Tame the build cache
On servers that build images, the build cache is almost always the biggest item. Every step of a Dockerfile ends up there so the next build goes faster. When a dependency changes, a new entry is created and the old one stays.
In our test, three builds left 15 entries with 394.7 MB in the cache. On our real server it was 111 entries with 5.6 GB, 5.5 GB of it marked as reclaimable. We didn’t delete anything there for this article, because builds run on that machine and we didn’t want to take their cache away just like that. The number is reported as it was.
The commands:
docker builder prune # only unused, unreachable cache
docker builder prune -a # the entire cache
docker builder prune --filter "until=72h" # cache older than 3 days
docker builder prune --keep-storage 5GB # delete until at most 5 GB remain
What we measured:
| Command | Result |
|---|---|
builder prune --filter until=1h | 0 B, because everything was younger than an hour |
builder prune --keep-storage 150MB | 104.9 MB deleted, 289.8 MB stayed |
builder prune --reserved-space 150MB (right after) | 0 B, the newer option for the same idea found nothing more it wanted to remove |
builder prune -a | 289.8 MB in 0.21 seconds, cache 0 B afterwards |
Notably, --keep-storage 150MB did not clean down to 150 MB. It deleted one large entry and left 289.8 MB in place. The limit is a guideline, not an exact target, because Docker only removes whole entries and spares those others depend on. For a recurring job, --filter until=… is therefore more predictable.
builder prune -a is completely harmless for your data; the cache is just an accelerator. Afterwards, the next build takes as long as the very first one, once. On a server that rarely builds, that doesn’t matter. On a CI machine that builds a hundred times a day, keep the last few days.
Step 5: Volumes, the only dangerous bucket
Volumes contain data: databases, uploads, configuration. Whatever gets deleted here is gone, unless you have a backup. That’s why Docker is more careful here than anywhere else, and why you should be too.

Our test setup: an anonymous volume (created automatically when an image defines a VOLUME and you don’t specify one, as with redis, postgres or mariadb), a named volume on a stopped container, and a named volume with 30 MB that no container is attached to.
docker volume prune
Deleted Volumes:
2ad9e151f3bb22fd4a3944a8c9b286e5aaec855a3b0edacd59f6ecdfdcfb0c21
Total reclaimed space: 89B
Since Docker 23, docker volume prune without options deletes only anonymous volumes no container uses anymore. Both named volumes stayed, including the orphaned one with 30 MB.
docker volume prune -a
Deleted Volumes:
cachedata
orphan
Total reclaimed space: 31.46MB
With -a, named volumes are fair game too, as soon as no container references them. At that moment cachedata was no longer attached to any container, because we had removed the stopped container with container prune beforehand. That’s exactly the trap: order matters. Clean up containers first, then volumes with -a, and data that was attached to a (stopped) container a moment ago is gone. A docker compose down has the same effect, because it removes the containers.
Counter-test: we recreated cachedata, attached it to a container, stopped the container and ran volume prune -a again. Result: 0 B, volume stayed. A stopped container protects its volume; a deleted one doesn’t.
And what about docker system prune --volumes, which everyone warns about? We ran it with -a, so in its harshest form:
docker system prune -a --volumes -f
# Total reclaimed space: 401.8MB
docker volume ls
# local cachedata
The named volume stayed. --volumes on system prune corresponds to volume prune without -a, so anonymous volumes only. Good news for everyone running their database with a named volume. Bad news for anyone who started docker run -d postgres without -v: their data sits in an anonymous volume, and that’s exactly what --volumes sweeps away once the container is deleted.
Our rules for volumes:
- Every container with data gets a named volume or a bind mount. Never rely on the anonymous volume from the image.
- Never
volume prune -ain a cron job. We only clean up volumes by hand and by name:docker volume rm old-name. - Check what’s orphaned first:
docker volume ls -f dangling=true
docker system df -v | sed -n '/Local Volumes/,/Build cache/p'
- Back up before deleting, even if only briefly. How to do that for databases is in PostgreSQL backup and MySQL backup.
Step 6: Cap container logs
Logs don’t show up in docker system df at all, and prune doesn’t touch them. Yet they’re one of the most common reasons for a full disk. The default log driver json-file writes the complete output of every container into a file under /var/lib/docker/containers/<id>/<id>-json.log, with no size limit.

Our test: a container that prints a 200-character line every millisecond. That’s extreme, but an application in debug mode or in an error loop can get there.
docker inspect -f '{{.HostConfig.LogConfig}}' logger
# {json-file map[]} ← no options, so no limit
After about 30 seconds the log file was 7.6 MB. In 10 measured seconds, 2,482 KiB were added. Extrapolated: roughly 250 KB per second, so over 20 GB a day, from a single container.
The same container with a limit:
docker run -d --log-opt max-size=1m --log-opt max-file=2 myapp:latest
After 15 seconds there were exactly two files, 806 KB and 1.0 MB. It will never get bigger. Docker rotates on its own and deletes the oldest file.
For all new containers at once, this goes into /etc/docker/daemon.json:
{
"log-driver": "json-file",
"log-opts": {
"max-size": "50m",
"max-file": "3"
}
}
Then systemctl restart docker. The setting applies only to newly created containers. Existing ones keep their old configuration until you recreate them, with Compose for example via docker compose up -d --force-recreate. In Compose you can also set the limit per service:
services:
app:
image: myapp:latest
logging:
driver: json-file
options:
max-size: "50m"
max-file: "3"
And if the disk is full right now? You can empty a running container’s log file without stopping it:
truncate -s 0 /var/lib/docker/containers/<id>/<id>-json.log
In our test the file was back at 510 KB two seconds later (the container kept writing, after all), and docker logs worked normally afterwards. Do not delete the file with rm: Docker keeps it open, and the space is only freed when the container stops. That’s the same effect as deleted-but-still-open files, which we measured in Linux disk full. For log files outside Docker, logrotate is the right tool.
On our real server, the largest container log file was 7.2 MB, by the way. Logs weren’t the problem there, because the services write little. The limit is set anyway, because the one error loop that starts at three in the morning doesn’t announce itself.
docker system prune: the big broom
docker system prune
This is the command most people find first. It bundles several steps. Without options it deletes:
- all stopped containers
- all networks no container uses
- all dangling images
- the unused build cache
It does not delete: images in use, named images, volumes and logs.
Our measurement with three stopped containers, two empty networks and a filled build cache:
Deleted Containers: (3)
Deleted Networks:
unused2
unused1
Deleted build cache objects: (5)
Total reclaimed space: 105MB
real 0m 0.28s
The build cache went from 13 to 8 entries, from 289.8 to 184.9 MB. The remaining 184.9 MB was still reachable and stayed. No images disappeared, because the containerd store produced no dangling images.
The next levels:
docker system prune -a # also all images without a container
docker system prune -a --volumes # also anonymous volumes without a container
docker system prune -a --volumes freed another 401.8 MB in our test Docker; afterwards there were zeros everywhere, except for our named volume. On a developer laptop that’s a clean slate. On a server it means every stopped service downloads its image again on the next start.
Our recommendation: never run system prune -a blindly on servers. Use the individual commands with filters instead, so you know what happens.
Automatic Docker cleanup: the cron job
A server that regularly builds or updates images needs regular cleanup. This is the version we consider safe:

#!/bin/sh
# /usr/local/bin/docker-cleanup.sh
set -eu
echo "== $(date -Is) before"; docker system df
docker container prune -f --filter "until=168h" # stopped containers older than 7 days
docker image prune -a -f --filter "until=168h" # unused images older than 7 days
docker builder prune -f --filter "until=72h" # build cache older than 3 days
docker network prune -f --filter "until=168h"
echo "== after"; docker system df
chmod +x /usr/local/bin/docker-cleanup.sh
# cron: every Sunday at 04:30, output to a log
30 4 * * 0 /usr/local/bin/docker-cleanup.sh >> /var/log/docker-cleanup.log 2>&1
What’s deliberately missing: volumes. No cron job in the world should decide on its own which data can go. How to add the cron job and why >> … 2>&1 matters is in Create a cron job. If you prefer systemd, use a timer; the approach is described in Create a systemd service.
The until=168h filter has a catch you should know about: it refers to the image’s creation date, not when it was last used. An image built a month ago that was still running yesterday but isn’t today gets removed immediately. That’s usually exactly what you want, but with rarely used tool images it can be annoying.
What not to do during Docker cleanup
A few shortcuts look tempting and do damage:
- Never delete inside
/var/lib/dockeror/var/lib/containerdwithrm. Docker keeps track of which layer belongs to which image. Deleting things in there gives you broken images and containers that won’t start. The only exception: emptying log files withtruncate. - Don’t copy
docker rm -f $(docker ps -aq)from a forum. It also deletes all running containers. - Don’t run
docker volume prune -aafterdocker compose down. See above: at that moment your database volumes aren’t attached to any container. - Don’t only look at
docker images. The sizes there count shared layers multiple times. Added up, it looks like far more than would actually be freed. - Don’t confuse Docker Desktop with a server. On Windows and macOS everything lives in a virtual disk that doesn’t shrink automatically after cleanup. There are separate tools for that in the settings under “Resources”. This guide is for Linux servers.
Our order when the disk is full
When a server reports a full disk, this is what we do:
df -hand a quick check whether it’s really Docker (du -sh /var/lib/docker /var/lib/containerd).- Read
docker system df. The biggest bucket goes first. - Build cache:
docker builder prune -f. Harmless, often the biggest win. - Stopped containers: look at
docker ps -afirst, thendocker container prune -f. - Images:
docker image prune -a -f. Costs download time on the next start, nothing else. - Logs: find the largest log files (
du -sh /var/lib/docker/containers/*/*-json.log | sort -h | tail), empty them withtruncate, set a limit. - Volumes: only by hand, only by name, only after a backup.
Steps 3 to 5 together took under a second in our test. The thinking beforehand is the part that takes time, and the more important part.
The video (January 2026) runs through the cleanup at speed. If you follow along, keep the volume warning from this article in mind.
How we tested
So you can put the numbers in context:
- Environment: the official
docker:dindimage as a privileged throwaway container on one of our Linux servers, running Docker 29.10.1 with the containerd image store (overlayfs,io.containerd.snapshotter.v1). No prune command touched the real server’s Docker. - Test objects:
alpine:3.22,nginx:alpine,redis:7-alpine,node:22-alpineand a mini app (Node, Express, a 50 MB ballast file), rebuilt three times and later twice more. Containers: a running nginx, redis with a named and with an anonymous volume, a container with 100 MB in its writable layer, a logger printing one line per millisecond. - From the real server we only took readings from
docker system dfanddu. We deleted nothing there. - Afterwards: test container including its volume deleted,
docker:dindimage removed,docker system dfon the real server identical before and after.
An honest note: with the classic overlay2 store (older installations that haven’t been switched over), rebuilds still produce dangling images, and docker image prune does find something there. The volume and log commands behave the same on both stores.

Conclusion
Docker cleanup isn’t a single command; it’s five buckets with five rules. Build cache, stopped containers and unused images can be deleted without any risk to your data; at most it costs time on the next build or start. Logs you don’t clean up, you cap them, once in daemon.json, and then there’s peace. And volumes you only touch by name, never in bulk.
Take the two surprises from our measurement with you: with Docker 29’s containerd store, docker image prune often finds nothing anymore, because the space of old builds sits in the build cache. And system prune --volumes is more harmless than its reputation, but only for data in named volumes. If you started a database without -v, it lives in an anonymous volume, and that’s exactly what disappears in the next big clean-out.
How to set up several services with clean, named volumes is shown in our Docker Compose example. And if you want to know whether your containers aren’t just running but actually working, read Docker healthcheck.
Docker cleanup FAQ
How do I clean up Docker without losing data?
Use docker builder prune, docker container prune and docker image prune -a. None of these commands touches volumes. Your data lives in volumes or bind mounts and stays untouched. Delete volumes only deliberately with docker volume rm NAME.
What exactly does docker system prune do?
Without options it deletes stopped containers, unused networks, dangling images and unused build cache. With -a it adds all images without a container, with --volumes anonymous volumes without a container. Named volumes survive every variant; we verified that with Docker 29.10.1.
Is docker system prune safe?
Without -a and --volumes it’s safe for running services. It gets risky if you want to keep stopped containers on purpose or if data lives in anonymous volumes. Before running it, docker system df shows how much would be affected.
What’s the difference between docker image prune and docker image prune -a?
Without -a, only dangling images are deleted, meaning images without a name. With -a, every image no container uses, even if it has a name. Stopped containers count as users and protect their image.
Why does docker image prune find nothing for me?
With the containerd image store that Docker 29 uses on new installations, rebuilding under the same tag no longer creates dangling images. The space of old versions sits in the build cache. Use docker builder prune instead.
How do I clear the Docker build cache?
With docker builder prune (only unreachable entries) or docker builder prune -a (everything). For a targeted cleanup use --filter "until=72h". The cache holds no data, only intermediate steps; the next build just takes longer once.
Does docker volume prune delete my database?
Since Docker 23, docker volume prune without options only deletes anonymous volumes no container is attached to. With -a, named volumes without a container too. A stopped container protects its volume; a deleted one doesn’t, for example after docker compose down.
Why is less space freed than shown?
Images share layers. docker images shows each image’s full size, even if 99% of it is shared with others. In our test, two images were deleted and only 97 kB was freed. The realistic number is UNIQUE SIZE in docker system df -v.
How do I stop Docker logs from filling the disk?
Set max-size and max-file for the json-file log driver in /etc/docker/daemon.json, for example 50m and 3. It applies to newly created containers; existing ones must be recreated once. Without a limit, our test container wrote about 250 KB per second.
Can I just delete the Docker log file?
Not with rm, because Docker keeps the file open and the space is only freed when the container stops. Empty it with truncate -s 0 /var/lib/docker/containers/ID/ID-json.log. The container keeps running, and docker logs works normally afterwards.
Where does Docker store its data?
Containers, volumes and logs are under /var/lib/docker. With the containerd image store, images and layers are additionally under /var/lib/containerd. On our server that was 11 GB and 22 GB. docker info shows which backend is active.
How often should you clean up Docker?
On servers that regularly build or update images, once a week via a cron job with time filters (until=168h). On servers that rarely change, a look at docker system df once a month is enough. Volumes don’t belong in any automation.