How to Use Docker System Prune to Clean Up Your Docker Environment
Use
docker system prune to remove all unused containers, networks, images, and optionally volumes, freeing up disk space. Add -a to remove all unused images, not just dangling ones, and --volumes to include unused volumes in the cleanup.Syntax
The basic syntax of docker system prune is:
docker system prune: Removes stopped containers, unused networks, dangling images, and build cache.-aor--all: Also removes all unused images, not just dangling ones.--volumes: Removes unused volumes as well.-for--force: Skips confirmation prompt.
bash
docker system prune [OPTIONS]
# Options:
# -a, --all Remove all unused images, not just dangling ones
# --volumes Prune volumes
# -f, --force Do not prompt for confirmationExample
This example shows how to run docker system prune with options to remove all unused data including volumes without confirmation.
bash
docker system prune -a --volumes -f
Output
Deleted Containers:
123abc456def
Deleted Networks:
my_unused_network
Deleted Images:
unused_image_1
unused_image_2
Deleted Volumes:
my_unused_volume
Total reclaimed space: 500MB
Common Pitfalls
Common mistakes when using docker system prune include:
- Running without
-fand expecting no prompt. - Not using
-aand missing removal of unused images. - Forgetting
--volumesif you want to clean volumes, which are not removed by default. - Accidentally deleting data still needed by active containers.
Always review what will be deleted before confirming.
bash
docker system prune
# prompts for confirmation and removes only dangling images
docker system prune -a --volumes -f
# force removes all unused images, volumes, containers, and networks without promptQuick Reference
| Option | Description |
|---|---|
| docker system prune | Remove stopped containers, unused networks, dangling images, and build cache |
| -a, --all | Remove all unused images, not just dangling ones |
| --volumes | Remove unused volumes |
| -f, --force | Skip confirmation prompt |
Key Takeaways
Use
docker system prune to clean up unused Docker resources and free disk space.Add
-a to remove all unused images, not just dangling ones.Include
--volumes to also remove unused volumes.Use
-f to skip the confirmation prompt for faster cleanup.Always double-check what will be removed to avoid deleting needed data.