Home DockerHow to Clean Up Docker: Prune Images, Containers and Volumes

How to Clean Up Docker: Prune Images, Containers and Volumes

by Knowledge Base Linux
How to Clean Up Docker — How to Clean Up Docker: Prune Images, Containers and Volumes

This article covers How to Clean Up Docker in detail. “No space left on device” — on a Docker host, this error rarely means your applications grew. It usually means months of old images, stopped containers, dangling layers and forgotten volumes silently ate the disk. The docker prune family of commands is the broom: used correctly, it routinely reclaims tens of gigabytes in seconds. This guide shows you how to inspect what Docker is storing, prune each object type safely, filter by age and labels, and automate cleanup without risking running workloads. New to Docker? Install it first with our Ubuntu 24.04 guide.

Step 1: See Where the Space Actually Goes – How to Clean Up Docker

Never prune blind. This command is the map:

docker system df

# Verbose view: every image, container and volume with sizes
docker system df -v

The output separates images, containers, local volumes and build cache, and — crucially — shows how much of each is reclaimable. Compare it with df -h on the host to see Docker’s share of total disk usage. Also check the json-file logs of chatty containers, which live outside this accounting and can grow into gigabytes on their own.

Disk space reclaimed after running docker prune

Step 2: Prune Containers, Networks and Images Separately – How to Clean Up Docker

Each object type has its own prune command, giving you precise control:

# Stopped containers
docker container prune

# Networks not used by any container
docker network prune

# Dangling images only (untagged layers) - the safe default
docker image prune

# Unused volumes (careful: data lives here!)
docker volume prune

The commands ask for confirmation and report the total space reclaimed. Dangling images — layers left behind by rebuilds that no tag points to anymore — are the normal, zero-risk win: nothing references them by definition.

Step 3: The Big Lever — docker system prune

# Everything unused: stopped containers, unused networks,
# dangling images and build cache
docker system prune

# Aggressive: ALL images not used by a running container,
# plus volumes
docker system prune -a --volumes

The -a flag changes the game: images that are merely not currently used — including your tagged releases sitting idle — get deleted too. That is often exactly what a CI runner or a build server needs, and exactly what you must not run casually on a production host whose rollback plan depends on older images being present locally. Decide consciously, every time.

Step 4: Filter by Age and Label

Filters make pruning surgical instead of sweeping. The until filter only removes objects older than the given duration — the safest way to automate cleanup:

# Remove containers stopped more than 24h ago
docker container prune --filter "until=24h"

# Remove images created more than 7 days ago (and unused)
docker image prune -a --filter "until=168h"

# Protect important objects with labels, then exclude them
docker image prune -a --filter "label!=keep"

Label release images at build time (docker build --label keep ... is not a build flag — labels are set in the Dockerfile with LABEL keep=true) and your filters can spare them. The until filter uses creation time, giving recently built images a grace period even if nothing runs them yet.

Step 5: Clean the Build Cache

BuildKit cache is frequently the single largest consumer on build machines:

docker buildx du          # inspect cache usage
docker buildx prune       # remove unused cache
docker buildx prune --filter "until=72h"

On dedicated build servers, an age-filtered buildx prune in a nightly job keeps cache useful for fast rebuilds while capping its growth.

Automating Cleanup Safely

A conservative weekly cron job used by many sysadmins:

# /etc/cron.weekly/docker-cleanup
#!/bin/sh
docker container prune -f --filter "until=72h"
docker image prune -f --filter "until=168h" --filter "label!=keep"
docker network prune -f

Note what is deliberately absent: no -a on images, no volume pruning. Volumes hold data, and “unused” only means “not attached right now” — a database volume for a temporarily stopped service qualifies. Prune volumes manually, with your eyes open, after checking docker volume ls. If raw disk pressure is your recurring problem, also review our general guide on checking disk usage on Linux for the non-Docker side of the story.

Troubleshooting

Prune freed almost nothing. Your space is probably in volumes or logs. Check docker system df -v and the size of /var/lib/docker/containers/*/*-json.log.

Disk still full after pruning. Deleted files stay allocated while a process holds them open — restart the Docker daemon (systemctl restart docker) in a maintenance window to release them.

An image I need disappeared. It was unused and caught by a broad prune. Pull it again from your registry — and next time, label it or keep filters conservative.

Conclusion

Measure with docker system df, prune dangling objects freely, use age filters for automation, and treat volume pruning as a manual, deliberate act. Done regularly, docker prune turns a recurring 2 a.m. disk-full incident into a boring weekly chore. The official pruning documentation details every filter and object type.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.