What Does Docker Prune Do?
Learn what docker system prune does, how it reclaims disk space from unused containers and images, and why volumes are excluded by default here.
Expected Interview Answer
`docker system prune` (and its more targeted variants like `docker container prune`, `docker image prune`, `docker volume prune`, and `docker network prune`) removes unused Docker objects — stopped containers, dangling or unreferenced images, unused networks, and optionally unused volumes — to reclaim disk space.
By default, `docker system prune` removes all stopped containers, all networks not used by at least one container, all dangling images, and all build cache, but it leaves volumes untouched unless you pass the `--volumes` flag, since volumes often hold data you don't want deleted accidentally. Adding `-a` to an image prune or system prune also removes any image not currently referenced by a running container, not just dangling ones, which is much more aggressive. Because pruning is destructive and irreversible, Docker prompts for confirmation unless you pass `-f` / `--force`, and it's good practice to run `docker system df` first to see what space each category is actually using.
- Reclaims disk space consumed by stopped containers and unused images
- Cleans up dangling build layers left over from repeated builds
- Keeps volumes safe by default, preventing accidental data loss
- Targeted subcommands let you prune only what you intend to
- Useful maintenance step in CI runners and dev machines low on space
AI Mentor Explanation
Docker prune is like a groundskeeper clearing out equipment left behind from matches that finished long ago, tossing worn nets and unused stumps nobody has touched since the last tournament. Kit still assigned to a team currently playing on the ground is left completely untouched. Anything genuinely unreferenced by an active match gets cleared to free up the storage shed.
Step-by-Step Explanation
Step 1
Check current usage
Run docker system df to see how much space containers, images, volumes, and build cache are each using.
Step 2
Choose the right scope
Pick a targeted command like docker image prune or the broader docker system prune depending on what needs cleaning.
Step 3
Confirm or force
Docker prompts for confirmation before deleting anything, unless -f or --force is passed.
Step 4
Stopped containers removed
All containers not currently running are deleted, freeing their writable layers.
Step 5
Dangling images and cache cleared
Untagged, unreferenced images and unused build cache are removed; add -a to remove all unused images and --volumes to include unused volumes.
What Interviewer Expects
- Explains that prune removes unused containers, images, networks, and cache to reclaim space
- Knows volumes are excluded by default unless --volumes is passed
- Understands the difference between dangling images and all unused images (-a flag)
- Mentions checking docker system df before pruning
- Warns that pruning is destructive and irreversible
Common Mistakes
- Running docker system prune --volumes without checking for important data first
- Assuming prune removes running containers or their in-use images
- Confusing dangling images with all unused images
- Forgetting that pruning build cache can slow down the next build
Best Answer (HR Friendly)
“Docker prune is a cleanup command that removes leftover, unused files such as stopped containers and old images to free up disk space, similar to emptying a recycle bin. It is careful not to touch anything currently in use, and by default it also leaves stored data volumes untouched to avoid accidental data loss.”
Code Example
# See what space each category is using
docker system df
# Remove stopped containers, dangling images, unused networks, and build cache
docker system prune -f
# Also remove all unused images, not just dangling ones
docker system prune -af
# Include unused volumes too (be careful, this deletes data)
docker system prune -af --volumesFollow-up Questions
- What is the difference between a dangling image and an unused image?
- Why does docker system prune leave volumes alone by default?
- What does docker system df show before you decide to prune?
- How do targeted prune commands like docker image prune differ from docker system prune?
- What are the risks of running docker system prune --volumes in production?
MCQ Practice
1. What does `docker system prune` remove by default?
By default, system prune clears stopped containers, dangling images, unused networks, and build cache, but leaves volumes untouched.
2. How do you include volumes when pruning?
Volumes are excluded from prune by default and must be explicitly included with the --volumes flag.
3. What does adding -a to an image prune command change?
The -a flag broadens the prune to remove any unused image, not just untagged dangling ones.
Flash Cards
What does docker system prune do? — Removes unused containers, dangling images, unused networks, and build cache to free disk space.
Are volumes removed by default when pruning? — No, volumes are only removed if you explicitly pass --volumes.
Dangling image vs unused image: what's the difference? — A dangling image is untagged and unreferenced; an unused image (with -a) includes any image not used by a running container.
What command shows disk usage before pruning? — docker system df.