Docker Guide

Docker Image Cleanup Without Breaking Deployments

A practical guide to cleaning up Docker images, containers, and volumes without deleting useful data or disrupting active work.

Back to tutorials

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`.
commands
docker ps
docker ps -a
docker images
docker volume ls

This 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.

command
docker image prune

That removes dangling images only. If you want to remove all images that are not used by an existing container, use:

command
docker image prune -a

The `-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:

command
docker container prune

Docker 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.

command
docker volume prune

Only 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`.

command
docker system prune
command
docker system prune --volumes

The 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

commands
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