Difficulty: Beginner
How do you clean up unused Docker images, containers, networks, volumes and build cache, and what exactly does each prune command delete?
Use docker system prune to remove stopped containers, unused networks, dangling images and unused build cache in one go. Add -a to also remove every image not used by any container, and --volumes to include unused volumes. For finer control, use the per-object commands (docker container prune, docker image prune, docker volume prune, docker builder prune) and check space first with docker system df.
docker system df shows how much space images, containers, local volumes and build cache use, and how much of that is reclaimable. docker system df -v breaks it down per image and per volume. On a CI runner or a small cloud VM, build cache is very often the biggest item, not images.
| Command | Removes |
|---|---|
docker container prune | All stopped containers |
docker image prune | Dangling images only (untagged <none>:<none> left behind by rebuilds) |
docker image prune -a | Every image not used by at least one container |
docker network prune | Custom networks not used by any container |
docker volume prune | Unused anonymous volumes (add -a for unused named volumes too) |
docker builder prune | Dangling build cache (-a for all build cache) |
docker system prune | Stopped containers, unused networks, dangling images, unused build cache |
docker system prune -a --volumes | All of the above plus unused images and unused volumes |
Two details people get wrong:
docker system prune. This is deliberate: volumes hold data such as a Postgres database.docker volume prune only removes anonymous volumes by default. Named volumes like pgdata need docker volume prune -a. Older tutorials that say otherwise predate this change."Unused" means not referenced by any container, including stopped ones. A stopped container still holds its image and volumes, which is why pruning containers first frees more on the next step.
--filter "until=24h" only removes objects created more than 24 hours ago, which avoids deleting an image a teammate just built.--filter "label=env=ci" or label!=keep targets objects by label.-f / --force skips the confirmation prompt, which you need in cron jobs and CI.A typical nightly CI cleanup is docker system prune -af --filter "until=72h" plus docker builder prune -af --filter "until=72h". Avoid --volumes on any server that runs stateful containers.
docker system prune -a --volumes on a dev laptop to "fix" disk space and losing your local database volume./var/lib/docker. Docker tracks metadata separately; always clean up through the CLI.max-size and max-file in the log options instead of pruning around the problem.# How much space is used and reclaimable?
docker system df
# Stopped containers and dangling images older than a day
docker container prune -f --filter "until=24h"
docker image prune -f --filter "until=24h"
# Build cache usually frees the most on CI runners
docker builder prune -af --filter "until=72h"
# Unused named volumes too (careful: this deletes data)
docker volume prune -a
Check what is reclaimable first, then prune by type with an age filter so recent work is left alone.
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
Put this in /etc/docker/daemon.json and restart the daemon; it applies to newly created containers and stops logs from silently filling the disk.
docker system prune, Dangling images, Build cache, docker system df, Prune filters