How to Clean Up Docker Disk Space (Images, Volumes, Build Cache)

Docker's various caches, unused images, and stopped containers can consume substantial disk space over time. This guide covers systematically cleaning up without accidentally removing something you actually need.

Checking Docker's Current Disk Usage

docker system df

Breaks down disk usage by images, containers, volumes, and build cache — identifies which category is consuming the most space before deciding what to clean up.

Removing Stopped Containers

docker container prune

Removes all stopped containers — safe for containers you're genuinely done with, though verify nothing important is stopped-but-intended-to-restart before running this.

Removing Unused Images

docker image prune

Removes "dangling" images (untagged layers, typically leftover from rebuilds) — the safest image cleanup, unlikely to remove anything you're actually using.

Removing ALL Unused Images (More Aggressive)

docker image prune -a

Removes any image not currently used by a running container, not just dangling ones — more aggressive; verify you don't need any currently-stopped-container's image before running this.

Removing Unused Volumes

docker volume prune

Caution: removes volumes not referenced by any container — if a volume contains important data from a container you've temporarily stopped, this would delete that data; always verify carefully before running.

Clearing Build Cache

docker builder prune

Removes cached build layers — safe to clear, since it just means future builds are slightly slower (rebuilding cache) rather than any actual data loss.

The Comprehensive Cleanup Command

docker system prune -a --volumes

Removes stopped containers, unused networks, all unused images, and unused volumes in one command — the most aggressive option; use with real caution and understanding of exactly what it removes.

Always Verify Before Aggressive Cleanup

docker system df -v

Shows detailed breakdown including specific image/volume names and sizes — review this before running aggressive prune commands, especially --volumes, to confirm you're not about to remove something important.

Setting Up Regular, Safe Automated Cleanup

0 3 * * 0 docker container prune -f && docker image prune -f && docker builder prune -f

A weekly cron job for the safer cleanup operations (excluding volumes, which carry genuine data-loss risk if automated without careful review) — keeps routine buildup in check without risking accidental data loss.

Identifying Specific Large Images

docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k3 -h -r

Sorted by size, helping identify specific large images worth investigating — sometimes a single overly-large image (not optimized with multi-stage builds, see Docker Multi-Stage Builds: Reducing Image Size) is the primary disk usage culprit.

Checking for Orphaned Compose Project Resources

docker compose down --volumes --remove-orphans

Cleans up a specific compose project's resources including volumes — useful when decommissioning an entire application stack, though again, verify volume removal is genuinely intended.

Common Errors

Accidentally deleted needed volume data — if caught immediately and the underlying storage hasn't been overwritten, data recovery tools might help, but this isn't reliable; the real lesson is always reviewing carefully before volume-affecting prune commands, and maintaining proper backups regardless.

Continue Reading

Browse more articles in Docker & Containers.

  • docker cleanup disk space, docker system prune, docker remove unused images, docker disk usage
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Install Docker Engine on Ubuntu & Debian (Complete Guide)

Docker lets you package applications and their dependencies into lightweight, isolated containers...

How to Install Docker Compose on Ubuntu & Debian

Docker Compose lets you define and run multi-container applications from a single YAML file,...

Deploy Your First Docker Container: A Beginner's Walkthrough

With Docker installed, this hands-on walkthrough covers the core workflow you'll use constantly:...

How to Update Docker and Docker Compose Safely

Keeping Docker Engine and Compose updated brings security fixes, performance improvements, and...

Docker Compose Troubleshooting: Common Errors & Fixes

A reference guide to the Docker and Docker Compose errors you're most likely to encounter, with...