Cleanup Strategies with docker system prune
Master docker system prune strategies, flags like --all and --volumes, time-based filters, and automated cron setups to keep production Docker nodes clean and operational.
Published on • November 17, 2026
AI Assistant

Unmanaged Docker environments eventually exhaust host disk space due to accumulated stopped containers, unused networks, dangling images, and build caches. The docker system prune command provides a single, powerful entry point for cleaning up unused resources across the Docker daemon.
Basic Usage: Removing Standard Overhead
Running docker system prune without extra flags cleans safe, non-referenced resources:
docker system prune
By default, this command prompts for confirmation and removes:
- All stopped containers.
- All networks not used by at least one container.
- All dangling images (images without a repository tag, displayed as
<none>:<none>). - All build cache (BuildKit build layers).
Output example:
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
8f9e0d1c2b3a
Deleted Networks:
custom_bridge_net
Deleted Images:
deleted: sha256:4a3b2c1d0e9f...
Total reclaimed space: 4.12GB
Deep Cleaning with --all (-a) and --volumes
By default, docker system prune leaves tagged images intact if no container is actively using them, and it never touches volumes to prevent accidental data loss.
1. Removing All Unused Images (-a / --all)
To remove all images that are not referenced by an active (running or stopped) container—including tagged images like node:22-alpine or postgres:16:
docker system prune -a
Warning: Running
docker system prune -aon a developer workstation or CI node forces Docker to re-download base images on the next build or container start.
2. Including Unused Volumes (--volumes)
Anonymous and named volumes attached to stopped containers persist on disk unless explicitly pruned. To purge all volumes not used by at least one container:
docker system prune --volumes
To purge unused images, containers, networks, and volumes all at once without interactive prompts:
docker system prune -a --volumes --force
Precise Targeting with --filter
To prevent pruning recent build artifacts or operational logs required for troubleshooting, use the --filter flag.
Filter by Time (until)
The until filter accepts relative durations (e.g., 24h, 7d) or Unix timestamps.
# Prune resources created more than 24 hours ago
docker system prune --all --filter "until=24h" --force
# Prune volumes unused for more than 7 days (168h)
docker volume prune --filter "until=168h" --force
Filter by Label (label)
You can exempt specific containers or volumes from automated cleanup by adding labels:
# Keep containers or images labeled keep-around=true
docker system prune --filter "label!=keep-around=true" --force
Automating Server Maintenance via Cron and Systemd
Production Docker hosts require routine maintenance to prevent root partition disk exhaustion.
1. Shell Script (/usr/local/bin/docker-cleanup.sh)
#!/usr/bin/env bash
set -euo pipefail
LOG_FILE="/var/log/docker-cleanup.log"
echo "=== Docker Cleanup Started: $(date -u '+%Y-%m-%d %H:%M:%SZ') ===" >> "$LOG_FILE"
# Reclaim disk space safely keeping recent 48h artifacts
docker system prune --all --volumes --filter "until=48h" --force >> "$LOG_FILE" 2>&1
echo "=== Cleanup Complete. Current Usage: ===" >> "$LOG_FILE"
docker system df >> "$LOG_FILE" 2>&1
Make the script executable:
chmod +x /usr/local/bin/docker-cleanup.sh
2. Cron Job Setup
Add a cron job to /etc/crontab or root’s crontab (crontab -e) to run nightly at 03:00 AM:
0 3 * * * root /usr/local/bin/docker-cleanup.sh >/dev/null 2>&1
3. Systemd Timer Alternative (Recommended for Modern Linux)
Create /etc/systemd/system/docker-cleanup.service:
[Unit]
Description=Automated Docker System Prune
After=docker.service
Requires=docker.service
[Service]
Type=oneshot
ExecStart=/usr/bin/docker system prune --all --volumes --filter "until=48h" --force
Create /etc/systemd/system/docker-cleanup.timer:
[Unit]
Description=Run Docker Cleanup Daily at 3 AM
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
Enable and start the systemd timer:
systemctl daemon-reload
systemctl enable --now docker-cleanup.timer
Takeaway: Routine cleanup with docker system prune is essential for host longevity. Combine --all and --volumes with time-based filters (until=48h) inside cron or systemd timers to maintain a self-cleaning production Docker node without risking active data.