Docker cleanup gets risky when people jump straight to `docker system prune -a` without understanding what they are about to remove. Docker is conservative by default and does not automatically delete most unused objects. That is why disk usage grows over time, especially on development machines. The right fix is not “delete everything.” It is understanding which objects are safe to prune and which ones might contain work you still need.
This guide focuses on a safe cleanup sequence grounded in Docker’s official pruning documentation. If you are cleaning up before a release or while debugging deployment behavior, it also helps to review the deployment readiness audit so cleanup does not become an accidental outage.
Why Docker disk usage grows
Docker stores images, stopped containers, networks, build cache, and volumes. A development machine can accumulate all of these quickly. Even when a container is stopped, it still exists unless you remove it. Volumes are never removed automatically because doing so could destroy persistent data.
What to check before cleanup
- List running containers with `docker ps`.
- List all containers with `docker ps -a`.
- Check images with `docker images`.
- Check volumes with `docker volume ls`.
docker ps
docker ps -a
docker images
docker volume lsThis quick inspection step matters because cleanup should follow awareness. If a stopped container still matters, or if a volume belongs to a local database you care about, an aggressive prune can become a data-loss event.
Clean up unused images
Start with images, because image cleanup is usually the safest place to recover space. Docker’s official docs distinguish between dangling images and all unused images.
docker image pruneThat removes dangling images only. If you want to remove all images that are not used by an existing container, use:
docker image prune -aThe `-a` form is more aggressive. It is often safe on local development machines, but remember that rebuilding large images later may cost time and bandwidth.
Remove stopped containers
Stopped containers still consume writable layer space. If they are no longer useful, remove them:
docker container pruneDocker prompts for confirmation unless you add `-f`. This is a good default. If you have a local workflow that depends on restarting stopped containers later, check the list first with `docker ps -a`.
Prune unused volumes carefully
Volumes are where people get hurt. They often hold actual application data such as databases, uploaded files, or local cache you still care about. Docker never removes them automatically for exactly that reason.
docker volume pruneOnly run this when you are certain the unused volumes are genuinely disposable. If you are not sure, inspect your containers and Compose setup first.
When to use docker system prune
`docker system prune` is a convenient shortcut, but it is easy to treat it as a magic cleanup command. In practice, it removes stopped containers, unused networks, dangling images, and build cache. Volumes are not pruned unless you add `--volumes`.
docker system prunedocker system prune --volumesThe second command is far riskier. Use it only when you fully understand that unused volumes may contain data you expected to keep.
Common mistakes
- Running `docker system prune --volumes` without checking what volumes exist.
- Assuming a stopped container is already gone.
- Deleting all unused images right before a demo or release.
- Cleaning up shared development machines without confirming ownership.
If your goal is simply to free some space, start with the least destructive prune operations first. Images and stopped containers are often enough.
Command cheat sheet
docker ps
docker ps -a
docker images
docker volume ls
docker image prune
docker image prune -a
docker container prune
docker volume prune
docker system prune