DEV Community

Cover image for Cleaning Up Docker with Prune
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Cleaning Up Docker with Prune

Docker is a widely used containerization platform for building, running, and distributing applications, but as you develop and deploy containers over time, it accumulates unused objects like stopped containers, dangling images, unused volumes, and networks. These leftover resources consume disk space and can degrade system performance if not cleaned up. This guide explains how to use the docker prune command family to safely remove unused Docker resources, covering the differences between docker prune and manual deletion, how to use each prune variant, and how to avoid accidental data loss during cleanup. By the end, you'll have a repeatable set of commands for keeping your Docker host's disk usage under control.


What Is docker prune?

The docker prune command family removes unused Docker resources — stopped containers, dangling images, unused volumes, networks, and build cache — all in one go. It's faster and less manual than docker rm or docker rmi.

Feature docker rm / rmi docker prune
Removes one item at a time Yes No
Removes all unused resources at once No Yes
Requires specific names or IDs Yes No
Safer for manual deletions Yes No
Useful for quick cleanup No Yes

Use docker rm when you need precision, and docker prune when you want to clean everything efficiently.

Quick Reference

$ docker system prune
$ docker container prune
$ docker image prune
$ docker volume prune
$ docker network prune
$ docker builder prune
Enter fullscreen mode Exit fullscreen mode

Common Options for docker prune

Most docker prune commands support the following options:

Option Description
--force Skip confirmation prompts. Use carefully to avoid accidental deletions.
--filter Filter resources by criteria such as age. Example: --filter "until=24h" removes items older than 24 hours.
--all (For images) Remove all unused images, not just dangling ones.

Use filters to avoid removing recently created resources during cleanup.

Clean All Docker Resources with docker system prune

Removes all unused containers, networks, dangling images, and build cache. Syntax: docker system prune [OPTIONS].

1. Clean up all unused Docker resources interactively:

$ docker system prune
Enter fullscreen mode Exit fullscreen mode

2. Clean up all unused resources without confirmation:

$ docker system prune --force
Enter fullscreen mode Exit fullscreen mode

3. Clean up unused resources older than 48 hours:

$ docker system prune --filter "until=48h"
Enter fullscreen mode Exit fullscreen mode

When to use: after heavy development or container churn, when disk space is limited, or when no stopped containers or unused images are needed.

Clean Docker Containers with docker container prune

Removes only stopped containers — running containers are not affected. Syntax: docker container prune [OPTIONS].

1. Remove all stopped containers:

$ docker container prune
Enter fullscreen mode Exit fullscreen mode

2. Remove all stopped containers without confirmation:

$ docker container prune --force
Enter fullscreen mode Exit fullscreen mode

3. Remove stopped containers older than 24 hours:

$ docker container prune --filter "until=24h"
Enter fullscreen mode Exit fullscreen mode

When to use: after running short-lived containers during testing or development, when stopped containers pile up over time, or to free up space without affecting running containers.

Clean Docker Images with docker image prune

Removes unused Docker images. By default, it only removes dangling images — images that are untagged and not referenced by any containers. Syntax: docker image prune [OPTIONS].

1. Remove all dangling images:

$ docker image prune
Enter fullscreen mode Exit fullscreen mode

2. Remove all unused images (not just dangling ones):

$ docker image prune --all
Enter fullscreen mode Exit fullscreen mode

3. Remove unused images older than 72 hours:

$ docker image prune --all --filter "until=72h"
Enter fullscreen mode Exit fullscreen mode

When to use: after building or pulling many temporary images, when running out of disk space due to image layers, or to clean up old application builds not referenced by containers.

Clean Docker Volumes with docker volume prune

Removes volumes that are not referenced by any containers. Active or attached volumes are not affected. Syntax: docker volume prune [OPTIONS].

1. Remove all unused volumes interactively:

$ docker volume prune
Enter fullscreen mode Exit fullscreen mode

2. Remove all unused volumes without confirmation:

$ docker volume prune --force
Enter fullscreen mode Exit fullscreen mode

When to use: after deleting containers that previously used persistent volumes, when temporary or test volumes accumulate over time, or to recover disk space tied to orphaned data.

Warning: Volume data is not recoverable after deletion. Always verify which volumes are in use before pruning.

Clean Docker Networks with docker network prune

Removes Docker networks that are not connected to any running or stopped containers. Syntax: docker network prune [OPTIONS].

1. Remove all unused networks interactively:

$ docker network prune
Enter fullscreen mode Exit fullscreen mode

2. Remove all unused networks without confirmation:

$ docker network prune --force
Enter fullscreen mode Exit fullscreen mode

When to use: after removing containers created with docker-compose, when testing multi-container applications, or to clean up orphaned or auto-generated bridge networks.

Clean Docker Build Cache with docker builder prune

Removes the build cache created during image builds. By default, it only removes dangling (unused) cache layers. Syntax: docker builder prune [OPTIONS].

1. Remove dangling build cache interactively:

$ docker builder prune
Enter fullscreen mode Exit fullscreen mode

2. Remove all unused build cache without confirmation:

$ docker builder prune --all --force
Enter fullscreen mode Exit fullscreen mode

When to use: after frequent image builds during development, when low on disk space, or to clean up intermediate cache layers that are no longer referenced.

Troubleshooting Docker Prune Issues

Docker Prune Doesn't Remove Everything

Cause: Docker only removes unused resources. If a container or volume is still referenced, it won't be pruned.

Solution:

1. Stop the container:

$ docker container stop <container_id>
Enter fullscreen mode Exit fullscreen mode

2. Remove the container:

$ docker container rm <container_id>
Enter fullscreen mode Exit fullscreen mode

3. Run prune again:

$ docker system prune
Enter fullscreen mode Exit fullscreen mode

Cannot Delete Volume Because It's In Use

Cause: The volume is still attached to a container.

Solution:

1. List all volumes:

$ docker volume ls
Enter fullscreen mode Exit fullscreen mode

2. Inspect the volume to see where it's used:

$ docker volume inspect <volume_name>
Enter fullscreen mode Exit fullscreen mode

3. Remove the attached container:

$ docker container rm <container_id>
Enter fullscreen mode Exit fullscreen mode

4. Then prune volumes:

$ docker volume prune
Enter fullscreen mode Exit fullscreen mode

Docker Disk Usage Still High

Cause: Some resources are still referenced or were not covered by the last prune command.

Solution:

1. Check the disk usage summary:

$ docker system df
Enter fullscreen mode Exit fullscreen mode

2. Run targeted prune commands:

$ docker image prune
Enter fullscreen mode Exit fullscreen mode
$ docker volume prune
Enter fullscreen mode Exit fullscreen mode

3. Run a full cleanup (if safe to do so):

$ docker system prune --all
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Schedule docker system df and a filtered docker system prune --filter "until=24h" as a recurring job instead of running cleanup manually.
  • Standardize on --filter "until=Xh" across your team's cleanup scripts to avoid accidentally removing resources still in active use.
  • Add a docker volume ls check to your deployment pipeline before pruning volumes, since volume data isn't recoverable.
  • Review docker builder prune --all periodically on CI build hosts where cache layers accumulate quickly.

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)