User Agents

Docker Prune Explained: Safe Cleanup Guide

Docker prune is the command family that removes unused Docker objects—images, containers, volumes, and build cache—to reclaim disk space. This guide explains exactly what each prune command deletes, how to use them safely, and how to avoid accidentally removing data you still need.

What Docker Prune Actually Does

Docker stores everything in /var/lib/docker (on Linux) or in a virtual disk (on Docker Desktop). Over time, stopped containers, dangling images, unused volumes, and old build layers accumulate. The docker prune family deletes these unused objects. Each command targets a specific type:

  • docker image prune – removes dangling images (untagged) and, with -a, all unused images.
  • docker container prune – removes stopped containers.
  • docker volume prune – removes volumes not used by any container.
  • docker network prune – removes networks not used by any container.
  • docker builder prune – clears the build cache.
  • docker system prune – runs all of the above (except volumes by default) plus the build cache.

All prune commands prompt for confirmation unless you pass -f (force).

How to Run Each Prune Command

Here are the exact commands and what they delete:

  1. Remove stopped containers: docker container prune – deletes all containers in the exited state. Add -f to skip the prompt.
  2. Remove unused images: docker image prune – deletes dangling images (those with no tag and not referenced by any container). Add -a to also remove all images not used by at least one container.
  3. Remove unused volumes: docker volume prune – deletes volumes not mounted by any container. This is the most dangerous because volumes often contain database data.
  4. Remove unused networks: docker network prune – deletes networks not used by any container. This is safe because custom networks are recreated automatically by compose.
  5. Clear build cache: docker builder prune – removes the layer cache used by docker build. This slows down future builds but does not affect running containers.
  6. Everything at once: docker system prune – cleans containers, networks, images, and build cache. Add --volumes to include volumes.

Example with a filter to keep images younger than 24 hours:

docker image prune -a --filter "until=24h"

Safety: What You Should Never Prune

The biggest risk is deleting volumes that contain persistent data. A volume is considered “unused” only if no container currently references it. If you stop a container that uses a volume, the volume becomes unused and docker volume prune will delete it, permanently erasing the data. Always back up volumes before pruning, or use a filter to exclude volumes with specific labels. For example:

docker volume prune --filter "label!=keep"

This keeps volumes that have the label keep. You can label a volume at creation: docker volume create --label keep mydata.

Another common mistake is pruning images with -a while you have a docker-compose.yml that references a tag like latest. If the image is not currently used by a running container, it will be deleted, and the next docker compose up will pull it again or fail if the registry is unavailable.

How to Choose What to Prune

Before running any prune, check what you are about to delete:

  • docker system df – shows disk usage by type and how much is reclaimable.
  • docker ps -a – lists all containers, including stopped ones.
  • docker images -a – lists all images, including dangling ones.
  • docker volume ls -f dangling=true – lists volumes not used by any container.
  • docker network ls – lists networks; the default bridge, host, and none are never pruned.

If you are low on disk space, start with docker system prune (no volumes) – it is safe and reclaims the most space. If you still need more, then consider volumes, but only after verifying they are truly disposable.

Common Problems and Misconceptions

“Prune deleted my named volume!” – Yes, if no container uses it. Always double-check with docker volume ls -f dangling=true.

“I ran system prune and my images are gone.” – That is expected if you used -a. The default docker system prune only removes dangling images, not all unused ones.

“Prune is slow.” – It can be, especially with many images or a large build cache. It is normal; the process is I/O heavy.

“I can’t prune because a container is using it.” – Correct. The command will skip objects in use. You can force removal with docker rm -f but that is dangerous.

“Prune removes everything, so I don’t need to back up.” – False. Volumes are not backed up by prune; they are deleted. Always back up before pruning volumes.

Comparing Prune with Manual Deletion

You can manually delete objects with docker rm, docker rmi, docker volume rm, and docker network rm. Prune is more convenient because it automatically selects unused objects. However, manual deletion gives you more control. For example, to delete a specific volume, you run docker volume rm myvol – prune would delete all unused volumes, which might be more than you want. In general, use prune for cleanup and manual commands for targeted removal.

Costs and Disk Space Impact

Docker images and build cache can consume tens of gigabytes. The build cache is especially large because each RUN in a Dockerfile creates a layer. Pruning the build cache can free a lot of space but will make the next build slower because it has to re-download base images and re-run steps. There is no monetary cost to prune, but there is a time cost: rebuilding images takes longer. If you use Docker Desktop, pruning also reduces the size of the virtual disk, which may help performance on macOS and Windows.

Frequently Asked Questions

Will docker system prune delete my running containers? No. It only removes stopped containers. Running containers are never touched.

Does prune remove images used by stopped containers? No. Only images that are not referenced by any container (including stopped ones) are removed with -a. A stopped container still references its image, so the image is kept.

How do I prune everything including volumes? Run docker system prune -a --volumes. This removes all unused images, stopped containers, unused networks, unused volumes, and the build cache. Use with extreme caution.

Can I undo a prune? No. Once deleted, images, containers, and volumes are gone. There is no trash bin. You must re-pull images or restore from backup.

Why is my disk still full after prune? Prune only removes Docker objects. Other files (logs, core dumps, other applications) remain. Check docker system df to see if Docker is actually the culprit.

Is it safe to prune while containers are running? Yes, it is safe. Prune skips objects in use. However, running a prune during a build may remove the build cache and slow the build.