0
0
Dockerdevops~5 mins

System prune for cleanup in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your computer fills up with unused Docker data like stopped containers, unused images, and networks. This can slow things down or waste space. The system prune command helps clean up all this unused data in one go.
When your disk space is low and you want to free up space used by Docker.
After testing many containers and images that you no longer need.
When you want to remove all stopped containers and unused images to keep your system tidy.
Before backing up your system to reduce the backup size.
When Docker commands become slow due to too many leftover resources.
Commands
This command removes all stopped containers, unused networks, dangling images, and build cache without asking for confirmation because of the -f flag.
Terminal
docker system prune -f
Expected OutputExpected
Deleted Containers: <container_id_1> Deleted Images: <image_id_1> Deleted Networks: <network_id_1> Total reclaimed space: 500MB
-f - Force the prune without confirmation prompt
This command shows how much disk space Docker is using before and after cleanup to verify the prune worked.
Terminal
docker system df
Expected OutputExpected
TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 10 2 2.5GB 1.5GB (60%) Containers 5 1 300MB 200MB (66%) Local Volumes 3 3 1GB 0B (0%) Build Cache 0 0 0B 0B
Key Concept

If you remember nothing else from this pattern, remember: docker system prune cleans up all unused Docker data to free disk space quickly.

Common Mistakes
Running docker system prune without the -f flag and then not confirming the prompt.
The command waits for user confirmation and does nothing until you type 'y'.
Use the -f flag to skip the confirmation if you want to automate or speed up the cleanup.
Expecting docker system prune to remove all images including those used by running containers.
It only removes dangling and unused images, not images used by running containers.
Stop and remove containers first if you want to remove their images.
Summary
Use docker system prune -f to remove all stopped containers, unused networks, dangling images, and build cache.
Check disk space usage before and after cleanup with docker system df.
Add the -f flag to avoid confirmation prompts during cleanup.