0
0
DockerHow-ToBeginner · 3 min read

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.
  • -a or --all: Also removes all unused images, not just dangling ones.
  • --volumes: Removes unused volumes as well.
  • -f or --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 confirmation
💻

Example

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 -f and expecting no prompt.
  • Not using -a and missing removal of unused images.
  • Forgetting --volumes if 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 prompt
📊

Quick Reference

OptionDescription
docker system pruneRemove stopped containers, unused networks, dangling images, and build cache
-a, --allRemove all unused images, not just dangling ones
--volumesRemove unused volumes
-f, --forceSkip 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.