Docker fills your disk while you're not looking. Every image you pulled for a test and never cleaned up. Every build that added another cache layer. Every container that exited and sat in the Exited state for six months. Run docker system df right now — if you haven't pruned recently, the reclaimable column is probably larger than you think.
What Does the Docker Cleanup Script Look Like?
What each step does
command -v docker— fails cleanly with a meaningful message if Docker isn't installed, rather than producing confusing "command not found" errors mid-script.FORCE="${1:-}"— optional--forceflag skips the confirmation prompt for cron and CI use. Always run interactively first to see what will be removed.docker system df— the RECLAIMABLE column is the number to watch. Run this before committing to any prune on a server you're unfamiliar with.docker container prune -f— removes containers in theExitedstate. Running containers are not affected.docker image prune -af --filter "until=720h"—-aremoves all unused images,--filter "until=720h"limits it to images not referenced or used in the last 30 days.docker volume prune -f— only removes volumes with no container attached, running or stopped. A Postgres data volume attached to a stopped container is safe.docker builder prune -af— clears the BuildKit build cache. Often the largest single category of reclaimed space on CI servers.
How Much Space Will This Free?
Run docker system df before committing to any prune. The output breaks down disk usage into four categories and shows the reclaimable amount for each:
Build cache is almost always 100% reclaimable. Images show the highest absolute savings on build servers. On a development machine that hasn't been pruned in six months, totals over 20GB are not unusual.
What Are the Common Variations?
Nuclear option — remove everything (development machines only):
Remove only dangling images (safer, not all unused):
Check what would be removed without deleting:
How Do I Schedule Docker Cleanup Automatically?
Check disk usage before every prune on production
The docker system df output at the start of the script shows the reclaimable amount before anything is deleted. On a production server you've never pruned before, review that output manually the first time. Build cache and stopped containers are safe to remove. Volumes require a moment's thought — if you're unsure whether a stopped container's volume holds live data, check docker ps -a before running. For disk emergencies where Docker isn't the culprit, the find large files script locates the actual offender in under 10 seconds.
Frequently Asked Questions
Will docker image prune delete images used by running containers?
No. Docker never removes images referenced by any container — running or stopped (but not removed). The -a flag removes all images not referenced by any container at all.
Is docker volume prune safe on a production server?
Only volumes with zero attached containers are removed. A database volume attached to a stopped-but-preserved container is not touched. A volume from a container you already removed with docker rm will be deleted — that is expected behavior.
What does --filter "until=720h" do?
720 hours equals 30 days. This restricts image removal to images not used in the last 30 days — a conservative safety margin that protects recently active images while cleaning up old test builds.
What is the difference between this and docker system prune?
docker system prune -af deletes everything in one shot with no visibility into what each category costs. This script separates each operation, shows before/after disk usage, and keeps a 30-day safety window on images.
How much space does this typically free?
On a CI/CD server without regular cleanup: 20–50GB is common. On a development machine: 5–15GB. The docker system df output at the start shows the exact reclaimable amount before you commit.
Part of the Disk Management guide
Running Docker on a DigitalOcean droplet? This script keeps your disk from hitting 100%.
Get $200 free credit — DigitalOcean
Get $200 Free →Affiliate link · we earn a commission
Related Scripts
- Find Large Files in Linux — du + find to locate the biggest disk space offenders fast when your disk hits 100%
- Delete Old Log Files — find -mtime cleanup for /var/log before logs fill the filesystem
- Disk Space Warning — df threshold check that exits with code 1 when a partition crosses your limit