Docker Disk Space Cleanup

Docker can consume gigabytes of disk space with unused images, containers, volumes, and build cache. This guide teaches you how to analyze disk usage and safely clean up unnecessary resources.

Analyze Usage Remove Unused Volume Cleanup Build Cache
Understanding Docker Disk Usage

Docker stores data in several locations. The main storage location is `/var/lib/docker` on Linux. This directory contains images, containers, volumes, networks, and build cache. Over time, unused resources accumulate and consume disk space.

The first step in cleanup is understanding what's taking up space. Docker provides built-in commands to analyze disk usage by type.

# Show high-level disk usage docker system df # Show detailed breakdown of all resources docker system df -v # Check Docker directory size (Linux) sudo du -sh /var/lib/docker # Check specific directories sudo du -sh /var/lib/docker/overlay2 sudo du -sh /var/lib/docker/volumes
Run `docker system df -v` regularly to monitor disk usage. It shows exactly how many containers, images, volumes, and build cache are consuming space.
docker system prune: The All-in-One Cleanup

The `docker system prune` command removes all stopped containers, unused networks, dangling images, and build cache. It's the quickest way to free up space.

# Remove stopped containers, unused networks, dangling images, build cache docker system prune # Remove all unused resources (including images without tags) docker system prune -a # Remove volumes as well (caution: data loss) docker system prune -a --volumes # Force without confirmation prompt docker system prune -f # Filter by time (remove resources older than 24 hours) docker system prune --filter "until=24h"
`docker system prune -a --volumes` removes ALL unused volumes, which may delete important persistent data. Use with caution in production.
Removing Containers
# Remove a specific stopped container docker rm container_name # Force remove a running container docker rm -f container_name # Remove all stopped containers docker container prune # Remove all stopped containers (no prompt) docker container prune -f # Remove containers based on exit status docker rm $(docker ps -a -q --filter "status=exited") # Remove containers created more than 24 hours ago docker container prune --filter "until=24h" # Remove all containers (running and stopped) - DANGEROUS docker rm -f $(docker ps -aq)
Removing Images
# Remove a specific image docker rmi image_name:tag # Remove force (if container using it) docker rmi -f image_name # Remove dangling images (untagged) docker image prune # Remove all unused images docker image prune -a # Remove images older than 24 hours docker image prune -a --filter "until=24h" # Remove images by pattern docker images | grep "none" | awk '{print $3}' | xargs docker rmi # Remove all images (DANGEROUS) docker rmi -f $(docker images -q)
Removing Volumes

Volumes persist data even after containers are removed. They often take up the most space but also contain important data. Be careful when removing volumes.

# List volumes docker volume ls # Inspect volume docker volume inspect volume_name # Remove a specific volume (container must be stopped) docker volume rm volume_name # Remove all unused volumes docker volume prune # Force remove unused volumes docker volume prune -f # Remove all volumes (including used) - DANGEROUS docker volume rm $(docker volume ls -q) # Remove volumes not used by any container docker volume prune --filter "label!=keep"
Volumes contain persistent data. Never run `docker volume prune` on production databases without backup. Data loss is permanent.
Removing Build Cache

Docker build cache stores intermediate layers from image builds. Over time, this can consume significant disk space, especially in CI/CD environments.

# Remove build cache docker builder prune # Remove all build cache (no prompt) docker builder prune -f # Remove build cache older than 24 hours docker builder prune --filter "until=24h" # Remove all build cache (aggressive) docker builder prune -a # View build cache usage docker system df docker buildx du
Cleaning Up Networks
# List networks docker network ls # Remove unused networks docker network prune # Remove specific network docker network rm network_name # Remove all unused networks docker network prune -f
One-Liner Cleanup Commands

docker system prune -a

Remove all unused containers, networks, images (both dangling and unreferenced)

docker system prune -a --volumes

Remove everything + volumes (most aggressive)

docker container prune -f

Remove all stopped containers

docker image prune -a -f

Remove all unused images

docker volume prune -f

Remove all unused volumes

docker builder prune -a -f

Remove all build cache

Automatic Cleanup Strategies

Set up automatic cleanup to prevent disk space issues:

# Crontab for daily cleanup (Linux) # Edit crontab: crontab -e # Run daily at 2 AM 0 2 * * * docker system prune -f # Run weekly, keep images from last 7 days 0 3 * * 0 docker image prune -a --filter "until=168h" -f # Docker daemon configuration for log rotation (/etc/docker/daemon.json) { "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } } # Set image retention in CI/CD # In GitHub Actions: add prune step after builds - name: Clean up Docker run: docker system prune -f
Finding What's Taking Space
# Show top 10 largest images docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | sort -k3 -h -r | head -10 # Show containers with their sizes docker ps -as # Show volumes with their sizes docker run --rm -v /var/run/docker.sock:/var/run/docker.sock alpine/du:latest # Find large files in Docker directory (Linux) sudo find /var/lib/docker -type f -size +100M -exec ls -lh {} \; # Show disk usage by Docker component docker system df -v | grep -E "Images|Containers|Volumes|Build Cache"
Best Practices for Disk Space Management
  • Run `docker system prune -a` weekly in CI/CD environments
  • Configure log rotation in daemon.json to limit log sizes
  • Use `--rm` flag for temporary containers
  • Use multi-stage builds to reduce image sizes
  • Set resource limits on containers to prevent runaway logs
  • Monitor disk space with alerts
  • Use `.dockerignore` to avoid sending unnecessary files
  • Regularly review and remove old volumes
  • Use named volumes for important data, anonymous volumes for temporary data
Frequently Asked Questions
Why is my Docker using so much disk space?
Common causes: old stopped containers, dangling images, unused volumes, build cache, and large log files. Use `docker system df -v` to identify the culprit.
Is it safe to delete /var/lib/docker?
No! This deletes ALL Docker data including images, containers, and volumes. Only do this as a last resort if you want to completely reset Docker. Always backup important volumes first.
How do I clean up Docker on Windows/macOS?
Docker Desktop has a built-in cleanup tool: Troubleshoot → Clean / Purge data. You can also run `docker system prune` from the terminal. For macOS, the Docker VM location is `~/Library/Containers/com.docker.docker/Data/`.
What's the difference between dangling and unused images?
Dangling images have no tag (like `:`). Unused images are any images not used by any container. `docker image prune` removes dangling images. `docker image prune -a` removes all unused images.
How often should I clean up Docker?
For development: weekly. For CI/CD: after every build. For production: setup monitoring and clean when disk usage exceeds threshold (e.g., 80%). Use scheduled crontab for automatic cleanup.
Can I recover data after `docker volume prune`?
No. Volume data is permanently deleted. Always backup important volumes before pruning.
Why does `docker system df` show more than `du -sh /var/lib/docker`?
`docker system df` includes resources in use by Docker that may not be in `/var/lib/docker` (like overlay mounts). It's the more accurate metric.
How do I limit Docker log sizes?
Configure log rotation in `/etc/docker/daemon.json`: `{"log-driver": "json-file", "log-opts": {"max-size": "10m", "max-file": "3"}}`. Restart Docker after changes.
Previous: Common Docker Mistakes Next: Docker Interview Questions

Regular disk space cleanup is essential for maintaining healthy Docker environments. Use these commands to reclaim gigabytes of storage and prevent disk full errors.