How to Clean Up Docker Resources: Containers, Images, Volumes, Networks
Use
docker system prune to remove unused containers, networks, images, and optionally volumes. For more control, use commands like docker container prune, docker image prune, and docker volume prune to clean specific resource types.Syntax
The main commands to clean Docker resources are:
docker system prune [options]: Removes all unused containers, networks, images, and optionally volumes.docker container prune: Removes all stopped containers.docker image prune [options]: Removes unused images.docker volume prune: Removes unused volumes.docker network prune: Removes unused networks.
Options like -a remove all unused images, not just dangling ones, and --volumes includes volumes in docker system prune.
bash
docker system prune [--volumes] [-a] docker container prune docker image prune [-a] docker volume prune docker network prune
Example
This example shows how to remove all stopped containers, unused images, unused networks, and unused volumes to free up space.
bash
docker system prune --volumes -a
Output
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated to them
- all build cache
- all unused volumes
Are you sure you want to continue? [y/N] y
Deleted Containers:
<container_id_1>
Deleted Images:
<image_id_1>
Deleted Volumes:
<volume_id_1>
Deleted Networks:
<network_id_1>
Total reclaimed space: 1.2GB
Common Pitfalls
Common mistakes when cleaning Docker resources include:
- Running
docker system prunewithout--volumesand expecting volumes to be removed (they are not removed by default). - Using
docker image prunewithout-aonly removes dangling images, not all unused images. - Accidentally deleting important volumes or images by running prune commands without checking what will be removed.
- Not confirming the prompt and expecting automatic cleanup.
bash
Wrong way:
docker system prune
# Volumes are NOT removed by default
Right way:
docker system prune --volumes -a
# Removes all unused containers, images, networks, and volumesQuick Reference
| Command | Description |
|---|---|
| docker system prune | Remove all stopped containers, unused networks, dangling images, and build cache |
| docker system prune --volumes | Also remove unused volumes |
| docker system prune -a | Remove all unused images, not just dangling ones |
| docker container prune | Remove all stopped containers |
| docker image prune | Remove dangling images |
| docker image prune -a | Remove all unused images |
| docker volume prune | Remove unused volumes |
| docker network prune | Remove unused networks |
Key Takeaways
Use
docker system prune --volumes -a to clean all unused Docker resources including volumes.Without
--volumes, volumes are not removed by docker system prune.Use specific prune commands for targeted cleanup like
docker container prune or docker volume prune.Always review the prompt before confirming to avoid deleting important data.
Pruning helps free disk space and keeps your Docker environment tidy.