Cleanup: Prune Without Regret
Reclaim disk from dangling images, stopped containers, unused volumes and build cache — surgically, so you never nuke data you need.
Docker quietly fills your disk: old images, exited containers, orphaned volumes, and an ever-growing build cache. Knowing exactly what each prune command removes is the difference between reclaiming 40GB and deleting a database volume by accident.
Know what each level removes
docker container prune— removes stopped containers. Safe.docker image prune— removes dangling images (untagged<none>layers). Add-ato also remove any image not used by a container — more aggressive.docker builder prune— clears the BuildKit build cache, often the biggest hog.docker volume prune— removes unused volumes. This deletes data — think twice.
The one-shot cleanup
docker system prune sweeps stopped containers, unused networks, dangling images and build cache in one go. Add -a --volumes only when you truly want a deep clean.
See where the space went
Start with docker system df — it shows reclaimable space per category so you prune the right thing instead of everything.
Example
# See what's using space and what's reclaimable
docker system df
# Targeted, safe cleanups
docker container prune -f # stopped containers
docker image prune -f # dangling images only
docker builder prune -f # build cache (often the biggest)
# One-shot sweep of everything unused (keeps volumes)
docker system prune -f
# Deep clean older than a week, including unused images
docker system prune -a --filter "until=168h" -f
# DANGER: also removes unused volumes (can delete data)
# docker system prune -a --volumesWhen to use it
- A build server runs docker system prune -f weekly to reclaim disk space consumed by dangling images and stopped containers from CI builds.
- A developer runs docker volume prune after a testing session to delete orphaned test database volumes taking up gigabytes.
- An ops engineer uses docker image prune -a --filter until=720h to delete images not used in the last 30 days, keeping only recent ones.
More examples
Prune everything unused at once
Reclaims all disk space from unused Docker objects in one command; add --volumes only after verifying no live data is stored.
# Remove stopped containers, dangling images,
# unused networks, and build cache
docker system prune -f
# Also remove unused volumes (CAUTION: check first!)
docker system prune -f --volumesPrune images older than 30 days
Filters pruning to images unused for over 720 hours (30 days), leaving recently used images intact.
# Remove all images not used by any container in 30 days
docker image prune -a --filter "until=720h" -f
# See how much space images are using first
docker system dfClean up build cache only
Targets only the BuildKit build cache for pruning, leaving images and containers untouched.
# Show how much build cache exists
docker buildx du
# Remove all build cache
docker buildx prune -f
# Remove build cache older than 7 days
docker buildx prune --filter until=168h -f
Discussion