
How to Clean Up Docker: Removing Unused Images, Containers, Volumes, and Networks
If you use Docker regularly for local development or server deployments, you will eventually notice your host machine's hard drive space shrinking.
Every time you execute a new docker build, update a database container, or download a base image, Docker stores historical revisions and intermediate layers on your disk.
Over time, this results in gigabytes of dangling images (labeled <none>:<none>), stopped container shells, unused virtual networks, and detached volumes.
In this guide, we will analyze Docker's disk footprint using built-in profiling tools and run cleanup commands to reclaim disk space safely.
Step 1: Inspect Your Docker Storage Footprint
Before deleting assets, check how much disk space is consumed by Docker components.
Run the docker system df command:
docker system dfYour terminal will display a summary table:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 4 8.42GB 5.20GB (61%)
Containers 12 2 120.4MB 110.2MB (91%)
Local Volumes 8 2 14.12GB 12.50GB (88%)
Build Cache 35 0 3.11GB 3.11GBThe "RECLAIMABLE" column tells you how much space you can retrieve by cleaning up inactive assets.
Step 2: Clear Stopped Containers
When you run docker stop, the container halts execution, but its file changes, metadata, and logs are preserved on your disk.
To list all stopped containers on your system:
# List all containers, including stopped ones
docker ps -aTo remove all stopped containers in a single command, use prune:
docker container pruneThis command permanently deletes all containers that are not currently running. It has no effect on running services.
Step 3: Clear Unused and Dangling Images
Docker images occupy the largest portion of storage. Images fall into two categories:
- Dangling Images: Images that have no tag and are not referenced by any container. They appear as
<none>:<none>in your list and usually occur when you build a new version of an image under the same tag. - Unused Images: Complete tagged images (e.g.,
redis:latest) that are not associated with any active containers.
Remove Dangling Images Only
To clear only untagged dangling image layers, run:
docker image pruneRemove All Unused Images (Dangling and Untagged)
To remove all images that are not associated with at least one running container, append the -a (all) flag:
docker image prune -a- Warning: Running this command deletes your local base image cache. If you run a service next time, Docker will have to download the base layers (e.g.,
node:18oralpine) from Docker Hub again.
Step 4: Clear Detached Volumes (Data Safety Check)
Docker Volumes store persistent data (like databases or upload directories). They are designed to survive even if their parent containers are deleted.
Because of this, deleting a container leaves its volume on your disk. These are called dangling/detached volumes.
- Warning: Volumes contain real database files. Ensure you possess backups of important databases before executing prune commands on volumes.
To clear all unattached volumes:
docker volume pruneStep 5: System-Wide Cleanup (The Ultimate Sweep)
Instead of running individual component commands, you can clean up containers, images, and networks simultaneously using the system prune utility:
docker system pruneThis single command removes:
- All stopped containers.
- All networks not used by at least one container.
- All dangling (untagged) images.
- All build caches.
The Deep Clean Command
By default, docker system prune does not delete volumes or unused (but tagged) images. To force a complete disk sweep:
# Deletes all unused images and detached volumes
docker system prune -a --volumesConfirm the warning prompt, and Docker will reclaim your storage space.
Conclusion
Docker disk consumption is managed by purging redundant container shells, cache layers, and detached volumes. Use docker system df to profile your storage allocations regularly, execute docker container/image prune to clear dangling items safely, and deploy docker system prune -a --volumes as a last resort to purge all unused images and configurations from your servers.