Docker Prune: Efficient Cleanup of Unused Docker Resources

Docker is a powerful containerization platform widely used to build, run, and share applications. Over time, as you develop and deploy, Docker may accumulate unused elements such as stopped containers, dangling images, unused volumes, and inactive networks. These remnants can consume valuable disk space and negatively impact performance if not removed.

This guide shows how to use the docker prune command family to safely eliminate unused Docker resources. You’ll see the difference between docker prune and manual deletion, how each prune variant works, and best practices to avoid data loss during cleanup.

What Is docker prune?

The docker prune command family helps remove unused Docker resources including stopped containers, dangling images, unused volumes, networks, and build cache in one go. It is faster and less tedious than manual removal with 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. Use docker prune for an efficient cleanup of everything at once.

The Short Answer Version

Below are the most common commands for removing unused Docker resources:

# Remove all unused containers, networks, images, and build cache
$ docker system prune

# Remove only stopped containers
$ docker container prune

# Remove dangling (untagged and unused) images
$ docker image prune

# Remove volumes not used by any container
$ docker volume prune

# Remove networks not connected to any container
$ docker network prune

# Remove Docker build cache
$ docker builder prune

Common Options for docker prune

Most docker prune commands support the following options:

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

Using filters helps ensure that recently created resources aren’t deleted during cleanup.

Clean All Docker Resources with docker system prune

The docker system prune command removes all unused containers, networks, dangling images, and build cache.

Command Syntax

docker system prune [OPTIONS]

Command Demonstration

Interactive cleanup of unused Docker resources:

Cleanup without confirmation:

$ docker system prune --force

Cleanup of resources older than 48 hours:

$ docker system prune --filter "until=48h"

When to Use

  • After intense development or frequent container changes
  • When disk space becomes tight
  • If no stopped containers or unused images are required

Clean Docker Containers with docker container prune

Use this command to remove only stopped containers. Running containers remain unaffected.

Command Syntax

docker container prune [OPTIONS]

Command Demonstration

Remove all stopped containers:

Remove all stopped containers without confirmation:

$ docker container prune --force

Remove stopped containers older than 24 hours:

$ docker container prune --filter "until=24h"

When to Use

  • After testing or development with short-lived containers
  • When stopped containers accumulate over time
  • To free up disk space without touching running containers

Clean Docker Images with docker image prune

This command removes unused Docker images. By default, it deletes dangling images — those that are untagged and not referenced by containers.

Command Syntax

docker image prune [OPTIONS]

Command Demonstration

Remove all dangling images:

Remove all unused images (not only dangling ones):

$ docker image prune --all

Remove unused images older than 72 hours:

$ docker image prune --all --filter "until=72h"

When to Use

  • After building or pulling multiple temporary images
  • When disk space is running low due to image layers
  • To remove outdated builds not linked to containers

Clean Docker Volumes with docker volume prune

This command deletes volumes not referenced by any containers. Active or attached volumes are not affected.

Command Syntax

docker volume prune [OPTIONS]

Command Demonstration

Remove all unused volumes interactively:

Remove all unused volumes without confirmation:

$ docker volume prune --force

When to Use

  • After deleting containers that used persistent volumes
  • When temporary or test volumes build up
  • To reclaim disk space tied to orphaned data

Warning: Volume data cannot be recovered once deleted. Always confirm which volumes are in use before pruning.

Clean Docker Networks with docker network prune

Use this command to remove Docker networks not connected to any running or stopped containers.

Command Syntax

docker network prune [OPTIONS]

Command Demonstration

Remove all unused networks interactively:

Remove all unused networks without confirmation:

$ docker network prune --force

When to Use

  • After removing containers launched with docker-compose
  • When testing multi-container environments
  • To clean orphaned or auto-generated bridge networks

Clean Docker Build Cache with docker builder prune

This command clears the build cache created during image builds. By default, it removes only dangling (unused) cache layers.

Command Syntax

docker builder prune [OPTIONS]

Command Demonstration

Remove dangling build cache interactively:

Remove all unused build cache without confirmation:

$ docker builder prune --all --force

When to Use

  • After frequent image builds during development
  • When disk space is low
  • To remove intermediate cache layers no longer needed

Troubleshooting Docker Prune Issues

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

Solution

Stop the container:

$ docker container stop <container_id>

Remove the container:

$ docker container rm <container_id>

Run prune again:

Cause: The volume is still attached to a container.

Solution

List all volumes:

Inspect the volume to see where it’s used:

$ docker volume inspect <volume_name>

Remove the attached container:

$ docker container rm <container_id>

Then prune volumes:

Cause: Some resources are still referenced or not included in the last prune command.

Solution

Check disk usage summary:

Run targeted prune commands:


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

$ docker system prune --all

Conclusion

In this article, you learned how to clean unused Docker resources using the docker prune command family. You explored methods for removing stopped containers, dangling images, unused volumes and networks, as well as build cache in a safe and efficient way. These commands help maintain a tidy development environment while reducing disk usage.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: