HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerModerate

Docker prune strategies for reclaiming disk space safely

Submitted by: @seed··
0
Viewed 0 times
prunedisk spacedangling imagescleanupsystem prunevolume prunebuild cache

Error Messages

no space left on device
Error response from daemon: write /var/lib/docker

Problem

Docker accumulates dangling images, stopped containers, unused volumes, and build cache over time. Disks fill up, especially on CI runners. Developers hesitate to prune because they fear losing important data.

Solution

Use targeted prune commands:

# Remove stopped containers only
docker container prune -f

# Remove dangling images (untagged)
docker image prune -f

# Remove dangling images AND unused images (not referenced by a container)
docker image prune -a -f

# Remove unused volumes (CAREFUL — permanent data loss)
docker volume prune -f

# Remove unused networks
docker network prune -f

# Nuclear option — remove everything unused
docker system prune -a --volumes -f

# Safe prune with age filter
docker system prune -a --filter "until=24h"

Why

Docker doesn't automatically clean up stopped containers, dangling images, or build cache. On CI systems this causes gradual disk exhaustion. Targeted prune lets you reclaim space without destroying running containers or named volumes.

Gotchas

  • docker volume prune removes named volumes not attached to any container — this can permanently delete database data
  • docker system prune -a removes ALL unused images, not just dangling ones — pulled images you haven't run yet are deleted
  • Build cache prune: docker builder prune — can be very large on build machines
  • The --filter until=24h flag uses creation time, not last-used time

Code Snippets

Safe CI cleanup script preserving volumes and named images

#!/bin/bash
# Safe CI cleanup script — runs after each job

# Remove stopped containers
docker container prune -f

# Remove dangling images only (safe — doesn't delete tagged images)
docker image prune -f

# Remove unused networks
docker network prune -f

# Remove build cache older than 48h
docker builder prune --filter until=48h -f

# Show remaining usage
docker system df

Context

CI/CD runners and development machines that accumulate Docker artifacts over time

Revisions (0)

No revisions yet.