Free disk space with docker system prune (safely)
Docker prune commands remove unused objects such as stopped containers, dangling images and build cache to free disk space.
- Updated Nov 27, 2026
- Markdown
See what Docker uses on disk and remove unused objects without touching your data.
- Docker installed
To free disk space safely, run docker system df to see what uses it, then prune one kind at a time: docker container prune, docker image prune and docker builder prune. Leave volumes alone unless you have a backup, because a volume is where your data lives.
How it works#
Docker stores everything under /var/lib/docker on a Linux host. Four kinds of object take space:
| Object | What it is | Removed by |
|---|---|---|
| Images | Layers pulled or built | docker image prune |
| Containers | Writable layers and logs of stopped containers | docker container prune |
| Build cache | Layers saved to speed up builds | docker builder prune |
| Volumes | Your data | docker volume prune (careful) |
Prune commands remove only unused objects. A dangling image is an untagged leftover (it shows as <none>), usually from an old build whose tag moved. An image is in use if any container, running or stopped, was created from it. That is why removing stopped containers first frees more images.
Volumes are the dangerous category. A volume counts as unused when no container references it, which includes a volume whose container you removed yesterday. That volume may hold your database. Container logs are a fifth source of growth: by default they are kept in a file per container with no size limit, and they are not removed by prune while the container exists.
A worked example, start to finish#
Picture a server that deploys by building an image under the same tag each time.
- Deploy 1 builds
app:latest. One image, one container. - Deploy 2 builds
app:latestagain. The tag moves to the new image, and the old one becomes a dangling image shown as<none>. The old container is removed, or stopped if you left it. - After many deploys,
docker image lsshows a list of<none>images, anddocker builder prunehas many old cache entries. docker system dfshows large RECLAIMABLE numbers for images and build cache.docker image pruneremoves the dangling images, anddocker builder pruneclears the cache.
Nothing here touches a volume, which is why this sequence is safe on a server with a database.
Step 1: See what is using space#
docker system dfWhat you should see: a table with rows for Images, Containers, Local Volumes and Build Cache, each with a size and a RECLAIMABLE column. Add -v for a per-object breakdown.
Also check the disk itself with df -h /.
Step 2: Remove stopped containers#
docker container pruneIt warns, asks for confirmation, and then removes every stopped container. Check docker ps -a first if a stopped container matters to you.
What you should see: the deleted container IDs and a line starting “Total reclaimed space”.
Step 3: Remove dangling images#
docker image pruneBy default this removes only dangling images. Adding -a removes every image not used by at least one container, including ones you pulled on purpose, which you will have to download again. To keep recent ones, add a filter such as --filter "until=168h".
Step 4: Remove build cache#
docker builder pruneThe next build is slower because it starts from scratch, but nothing breaks. Add -a to remove all unused cache instead of dangling cache alone.
Step 5: Everything at once (optional)#
docker system pruneThis combines stopped containers, unused networks, dangling images and build cache. It asks for confirmation, and it does not remove volumes.
Warning: volumes hold your data#
docker volume ls
docker volume prune
docker volume prune --all
docker system prune --volumesThese delete volumes that no container references. On current Docker versions, docker volume prune removes only unused anonymous volumes, while --all also removes named ones. Older versions removed named volumes too, so do not rely on the lesser behaviour. A volume whose container was removed (for example by docker compose down) counts as unused, so its data is deleted for good. Take a backup first (see Backups and the 3-2-1 rule), and read the list before you confirm. Also be careful with docker system prune -a, which removes all unused images.
Check it worked#
Run docker system df again. The numbers for the cleaned categories should be smaller, and each prune command prints “Total reclaimed space”. Then run df -h / to see the real disk. Finally docker ps and docker volume ls: your running containers and your volumes should be exactly as before.
Edge cases and gotchas#
- A stopped container keeps its image alive.
docker image prune -askips an image that a stopped container was created from. Rundocker container prunefirst. - Prune does not shrink logs. Container logs live with the container and can grow without limit. See Limit Docker log size.
- A stopped stack looks unused. If you ran
docker compose downon an app you will bring back, its images and networks look unused. A later prune removes images the stack needs, and the nextuppulls them again, which costs time and bandwidth. -fskips the question.docker system prune -fdeletes without asking. Fine in a scheduled job once you trust it, risky when typed by hand.- Rebuild after cache prune is slow. On a small server, a build from empty cache can take minutes and use memory.
- Do not delete under
/var/lib/dockerby hand. Docker tracks those files, and removing them directly can corrupt images. - Older versions differ. The volume prune behaviour changed in recent releases. Check
docker volume prune --helpon your machine.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
no space left on device during pull or build | The disk is full | Run docker system df, then prune what is safe |
| Image prune frees almost nothing | Images are used by stopped containers | docker container prune, then prune images again |
| Disk still full after prune | Logs, volumes or non-Docker files | sudo du -xh /var/lib/docker --max-depth=1, and limit log size |
Error response from daemon: conflict: unable to remove repository reference | A container still uses that image | docker rm the container first |
| Database gone after a prune | A volume was removed as unused | Restore from backup; in future back up before pruning volumes |
| Prune prompt shows more than you expected | You included -a or --volumes | Answer n and re-read the command |
docker system df is small but the disk is full | Something else is large: logs, other applications, or files outside Docker | Use df -h and sudo du -xh / --max-depth=1 to find it |
| A prune in a script waits forever | It is waiting for the y/n question | Add -f only after you have tested the command by hand |
| First build after pruning is slow | The build cache is gone | Expected, and it fills again |
Real-world scenario#
A small server with a 40 GB disk runs a few Compose apps. A deploy fails with no space left on device. You run docker system df and see that images and build cache account for most of the space, while the volume row is small and in use.
You run docker container prune, docker image prune and docker builder prune in that order, check df -h / and the apps still respond. You decide against --volumes, because one stopped app has a volume you might need. Then you add a weekly job that runs docker image prune -f and docker builder prune -f, and you set up log limits so the logs stop growing.
Quick reference#
| Task | Command |
|---|---|
| Show usage | docker system df, docker system df -v |
| Stopped containers | docker container prune |
| Dangling images | docker image prune |
| Unused images older than a week | docker image prune -a --filter "until=168h" |
| Build cache | docker builder prune |
| Containers, networks, dangling images, cache | docker system prune |
| Volumes (data loss risk) | docker volume prune, docker system prune --volumes |
Security and safety notes#
- Volume prune and
docker system prune --volumesdelete data permanently. Back up first and read the list. - Never schedule a prune with
-aor--volumeson a server whose stacks are sometimes stopped.
Related lessons#
- Image, container and registry explained: what dangling and unused images are
- Docker volumes and persistent data: why volumes need care
- Limit Docker log size: stop logs from filling the disk
- Check disk, memory and CPU usage: find what else uses space
Next#
Make Docker notice a broken app with Docker healthchecks.
Check what you learned
Frequently asked questions
Is docker system prune safe?
Without extra flags it removes stopped containers, unused networks, dangling images and build cache. It does not remove volumes. Read the confirmation prompt before you answer y.
Why did my database data disappear?
Most likely a volume prune removed a volume after its container was deleted, for example after docker compose down. Volumes hold data, and that deletion cannot be undone.
Docker said it reclaimed space but df still shows the disk full. Why?
A large share may be container logs or volumes, which system prune does not touch, or space held by files that a running process still has open. Check docker system df -v and the size of /var/lib/docker.
Can I delete files under /var/lib/docker by hand?
No. Docker tracks them internally, and removing files directly can corrupt images and containers. Use the docker prune and rm commands.
Sources
Keep going: guides and tutorials
- Deploy a Docker Compose App Self-Hosted: Web, Redis and Rollback Deploy a Docker Compose app on your own server with Levelrail: a web service and Redis, a bad release that does not take traffic, and a compose rollback.
- Docker Container Restarting in a Loop: 5 Causes, Fixed Five real reasons a Docker container keeps restarting, each reproduced with its exit code, the command that finds it and the fix. Then the same crash on Levelrail.
- Deploy an Express App to a VPS With Docker and Health Checks Deploy a Node.js Express API to your own server with no Dockerfile. Covers the port rule, a health route, secrets, deploy failure codes and rollback.