Skip to content

How to Clean Up Docker Disk Space (docker system prune)

DodaTech 2 min read

In this tutorial, you'll learn about How to Clean Up Docker Disk Space (docker system prune). We cover key concepts, practical examples, and best practices.

The Problem

You get No space left on device or Docker build fails with an out-of-space error. Docker caches stopped containers, unused images, dangling volumes, and orphaned networks over time. A few weeks of active development can consume tens of gigabytes. Each docker build creates intermediate layers, and each docker pull downloads images that are never deleted automatically.

Quick Fix

1. Run a full system prune

docker system prune -a --volumes

Expected output:

WARNING! This will remove:
  - all stopped containers
  - all volumes not used by at least one container
  - all networks not used by at least one container
  - all images without at least one container using them
Total reclaimed space: 12.4 GB

The -a flag removes all unused images (not just dangling ones). --volumes removes unused volumes. Without --volumes, volumes are preserved.

2. Dry run to see what would be removed

docker system prune -a --volumes --dry-run

This shows the reclaimable space without deleting anything. Run this first to understand the impact.

3. Prune selectively

# Remove only stopped containers
docker container prune

# Remove dangling images (untagged)
docker image prune

# Remove all unused images (not just dangling)
docker image prune -a

# Remove unused volumes
docker volume prune

# Remove unused networks
docker network prune

Selective pruning gives you finer control. Use docker image prune -a most often — images consume the most space.

4. Check current disk usage

docker system df

Expected output:

TYPE              TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images            23        6         10.2GB    8.1GB (79%)
Containers        15        3         456MB     398MB (87%)
Local Volumes     8         2         2.1GB     1.8GB (85%)
Build Cache       42        0         3.4GB     3.4GB (100%)

The RECLAIMABLE column shows what prune would free.

5. Add a cron job for automatic cleanup

# Run every Sunday at 3 AM
0 3 * * 0 docker system prune -a --volumes -f >> /var/log/docker-prune.log 2>&1

The -f flag skips the confirmation prompt, making it suitable for automation.

6. Limit build cache size

{
  "builder": {
    "gc": {
      "enabled": true,
      "defaultKeepStorage": "10GB"
    }
  }
}

Add this to /etc/docker/daemon.json and restart Docker. BuildKit garbage collection runs automatically when cache exceeds the limit.

7. Check which images consume the most space

docker images --format "{{.Size}}\t{{.Repository}}:{{.Tag}}" | sort -rh | head -10

Expected output:

1.2GB    python:3.12-slim
856MB    node:20-alpine
534MB    my-app:latest

This shows the largest images first. Identify images you no longer need and remove them with docker rmi.

Prevention

  • Run docker system prune -a --volumes weekly as a cron job
  • Set BuildKit garbage collection in daemon.json
  • Use --rm flag when running containers to auto-remove them on exit
  • Avoid pulling images you don't need for a specific task
  • Monitor disk usage with docker system df before each build
  • Use docker image prune -a more frequently than full system prunes

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro