- Difficulty:
- intermediate
Problem
Docker keeps every layer it has ever pulled. On a build machine that is tens of gigabytes within a month, and the first symptom is a build failing on a full disk at an inconvenient hour.
Trigger
Cron, Sundays at 04:00.
Prerequisites
- 01Docker installed, and permission to talk to the daemon.
- 02A writable /var/log, or a different path in the line.
- 03Nothing on the machine that depends on an image it cannot pull again.
Steps
- 01
See what would go first docker
docker system df shows reclaimable space per category.
- 02
Add the line to root's crontab cron
sudo crontab -e. Talking to the docker socket needs root or membership of the docker group.
- 03
Check the log after the first Sunday
tail /var/log/docker-prune.log shows what was deleted and how much space came back.
Payload · cron
# Sunday 04:00. Removes images, containers, networks and build cache
# untouched for a week. Volumes are left alone on purpose.
0 4 * * 0 /usr/bin/docker system prune --all --force --filter "until=168h" >> /var/log/docker-prune.log 2>&1Failure modes
- !--all removes images with no running container, including one a stopped container needs at its next start. Drop --all to keep tagged images.
- !An air gapped machine cannot re-pull what was pruned. Do not run this there.
- !The until filter is relative to image creation, not last use. An old base image in daily use is still removed and pulled again.
- !Volumes are untouched by design. Adding --volumes to this line deletes database data.
JSON-LD · HowTo
{
"@context": "https://schema.org",
"@type": "HowTo",
"name": "Reclaim disk space from Docker every Sunday",
"description": "A weekly cron line prunes Docker images, containers, networks and build cache untouched for seven days, and deliberately leaves volumes alone so data survives.",
"url": "https://automationsanonymous.com/automations/reclaim-disk-space-from-docker-weekly",
"tool": [
{
"@type": "HowToTool",
"name": "cron",
"url": "https://automationsanonymous.com/tools/cron"
},
{
"@type": "HowToTool",
"name": "docker",
"url": "https://automationsanonymous.com/tools/docker"
}
],
"step": [
{
"@type": "HowToStep",
"position": 1,
"name": "See what would go first",
"text": "docker system df shows reclaimable space per category.",
"url": "https://automationsanonymous.com/tools/docker"
},
{
"@type": "HowToStep",
"position": 2,
"name": "Add the line to root's crontab",
"text": "sudo crontab -e. Talking to the docker socket needs root or membership of the docker group.",
"url": "https://automationsanonymous.com/tools/cron"
},
{
"@type": "HowToStep",
"position": 3,
"name": "Check the log after the first Sunday",
"text": "tail /var/log/docker-prune.log shows what was deleted and how much space came back."
}
],
"datePublished": "2026-09-05T16:53:32.236Z"
}