Docker Prune — Cleaning Up Unused Docker Objects
What Is Docker Prune in Simple Terms?
Docker accumulates disk usage over time: stopped containers that were never removed, old image layers from previous builds (dangling images), volumes no longer attached to any container, and networks no longer used. The prune commands clean all of this up.
On a busy build machine that builds Docker images dozens of times per day, disk usage can grow by gigabytes daily without regular pruning.
Bash
# See current disk usagedocker system df# TYPE TOTAL ACTIVE SIZE RECLAIMABLE# Images 15 4 4.2GB 2.8GB (66%)# Containers 8 3 45MB 40MB# Local Volumes 5 3 1.5GB 800MB# Build Cache - - 2.1GB 2.1GBIndividual Prune Commands
Bash
# Remove all stopped containersdocker container prune# Removes: containers with status Exited or Created# Keeps: running containers # Remove dangling images (untagged — from old builds)docker image prune# Removes: images with no tag (<none>:<none>)# Keeps: tagged images # Remove ALL unused images (not just dangling)docker image prune -a# Removes: any image not referenced by a running container# Use carefully on production servers # Remove unused volumesdocker volume prune# Removes: volumes not attached to any container (running or stopped)# WARNING: this permanently deletes data in those volumes # Remove unused networksdocker network prune# Removes: networks not used by any container # Remove everything at once (containers, images, networks)# Does NOT remove volumes unless -v flag addeddocker system prune # Remove everything including volumes (DANGEROUS)docker system prune -a -v# WARNING: deletes ALL unused volumes = data loss riskSafe Pruning Patterns
Bash
# Safe daily prune — only removes stopped containers and dangling imagesdocker container prune -f && docker image prune -f # Skip confirmation prompts for automationdocker container prune -fdocker image prune -fdocker network prune -f # Prune images older than 48 hoursdocker image prune -a --filter "until=48h" # Add to crontab on build machines (run at 3am daily)# 0 3 * * * docker container prune -f && docker image prune -fWhat Is Safe to Prune
Bash
SAFE to prune: Stopped containers (docker container prune) Dangling images (docker image prune without -a) Unused networks (docker network prune) Build cache (docker builder prune) CAREFUL: All unused images (docker image prune -a) -> removes images not currently running -> next pull will re-download them DANGEROUS: Unused volumes (docker volume prune) -> permanent data deletion -> always verify before runningPLACEMENT PRO TIP**Tip:** Run `docker system df` before any prune command to see exactly how much space each category is using. This helps you decide which prune commands will have the most impact and avoid accidentally deleting volumes that contain important data.
COMMON MISTAKE / WARNING**Common Mistake:** Running `docker system prune -a -v` without checking which volumes exist. The `-v` flag removes ALL unused volumes — if a database volume is not attached to a running container at that moment (e.g., the database container is stopped for maintenance), the volume and all its data are permanently deleted.