Skip to main content

Docker Prune

Docker prune is a family of commands that delete unused Docker objects: stopped containers, dangling images, unused networks, build cache, and optionally volumes. It reclaims disk space, but volume pruning can permanently delete data.

What each prune removes

Docker never cleans up automatically, so stopped containers, old image layers, and cache accumulate, especially on CI and build hosts. Check usage first, then prune the narrowest category that solves the problem.

Bash
docker system df # usage by type, with reclaimable space
docker system df -v # per image, container, and volume

Commands

Bash
docker container prune # stopped containers
docker image prune # dangling images only
docker image prune -a # all images not used by any container
docker network prune # unused networks
docker builder prune # build cache
docker volume prune # unused volumes: data loss risk
docker system prune # containers, networks, dangling images, build cache
docker system prune --volumes # also volumes: dangerous

In Docker Engine 23.0 and later, docker volume prune removes only anonymous volumes unless you add --all.

Safe patterns

Bash
docker image prune -a --filter "until=48h" -f
# crontab on a build host, 3am daily
0 3 * * * docker container prune -f && docker image prune -f && docker builder prune -f --filter until=72h

Risk levels

  • Low: stopped containers, dangling images, unused networks, old build cache.
  • Medium: image prune -a, since the next pull re-downloads images.
  • High: any volume pruning, because a stopped database container leaves its volume "unused".
Common Mistake

Running docker system prune -a --volumes while a database container is stopped for maintenance. Its volume counts as unused and is deleted.

Tip

Run docker system df first and prune by category with age filters on shared hosts.

Frequently Asked Questions

Why do Docker hosts fill up with unused data?

Docker keeps stopped containers, replaced image layers, unused networks, and build cache until removed. Frequent CI builds can fill a disk within days without scheduled pruning.

Is docker system prune safe to run?

The default form is fairly safe, but it removes stopped containers and build cache and may slow the next builds. Adding -a or --volumes increases risk. Always review docker system df and avoid volumes unless you are sure.

How do I prune only old items?

Use filters, for example docker image prune -a --filter until=48h or docker builder prune --filter until=72h, which remove only items older than the given age.

Found a mistake, an outdated step or a broken link?