DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Docker Tutorial

Introduction to docker
Why Use Docker?
Docker vs Virtual Machines
Installing Docker
Key Docker Concepts
Docker Images
Docker Containers
Writing Dockerfiles
Docker Volumes

Cleanup & Pruning

PreviousPrev
Next

Keep your Docker environment clean. Remove stopped containers, reclaim disk space, and build automated cleanup habits.

Docker Cleanup & Pruning

Every container you run, every image you pull, every build you run, it all piles up quietly in the background. Left unchecked, it's not unusual to find Docker eating 20–30GB of disk space after a few weeks of active development. Therefore, it becomes important to clean it up safely.

See What's Taking Up Space First

Before deleting anything, check what you actually have:

# Show running containers
docker ps

# Show ALL containers, including stopped ones
docker ps -a

# Count how many stopped containers are sitting around
docker ps -aq -f status=exited | wc -l

# Disk usage breakdown across all Docker objects
docker system df

docker system df output:

TYPE            TOTAL   ACTIVE   SIZE      RECLAIMABLE
Images          14      5        4.2GB     2.8GB (66%)
Containers      23      3        156MB     156MB (100%)
Local Volumes   8       4        2.1GB     600MB (28%)
Build Cache     31      0        1.4GB     1.4GB (100%)

The RECLAIMABLE column is your target, it shows exactly how much space you can get back without affecting anything currently in use.

Removing Containers

# Remove a stopped container
docker rm my-container

# Remove multiple at once
docker rm web db cache

# Force remove a running container (in background, it stops first, then deletes)
docker rm -f my-container

# Remove a container and its anonymous volumes
docker rm -v my-container

You can't remove a running container without -f. Either stop it first with docker stop, or force-remove it with docker rm -f.

Remove All Stopped Containers at Once

# Remove all stopped containers (It prompts for confirmation)
docker container prune

# Skip the confirmation
docker container prune -f

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

--rm

The most effective way to avoid container buildup is to never let them accumulate in the first place. Add --rm to any container you don't need to keep. Use --rm for one-off commands, CI tasks, data migrations, quick tests or anything you won't need to revisit. It's the single best cleanup habit you can build.

# Container is automatically deleted the moment it exits
docker run --rm alpine echo "done"
docker run --rm -it ubuntu bash

Removing Images

Old and unused images are usually the biggest disk consumers:

# Remove a specific image
docker rmi nginx:1.24

# Remove multiple images
docker rmi nginx:1.24 node:18

# Force remove (even if a stopped container references it)
docker rmi -f old-image:v1

# Remove dangling images (untagged layers left over from builds)
docker image prune

# Remove ALL images not used by any container
docker image prune -a

# Skip confirmation
docker image prune -a -f

Dangling images are the untagged <none> images you see in docker images, they're leftover intermediate layers from previous builds. Safe to delete.

Removing Volumes

# Remove a specific volume
docker volume rm mydata

# Remove all volumes not attached to any container
docker volume prune

# Skip confirmation
docker volume prune -f

docker volume prune permanently deletes data. Always run docker volume ls first to make sure you're not about to wipe something important. There's no undo.

Removing Networks

# Remove a specific network
docker network rm my-network

# Remove all unused networks
docker network prune

Unused networks are safe to prune as they hold no data, just configuration.

Build Cache Cleanup

Build cache is almost always the biggest and safest thing to delete:

# Remove all build cache
docker builder prune

# Remove cache older than 48 hours
docker builder prune --filter "until=48h"

# Skip confirmation
docker builder prune -f

Build cache is 100% safe to delete. The only consequence is that your next build starts from scratch and takes longer, but subsequent builds will cache again normally.

The Nuclear Option: docker system prune

When you want to clean everything at once:

# Remove stopped containers, dangling images, unused networks, build cache
docker system prune

# Also remove unused volumes
docker system prune --volumes

# Also remove ALL unused images (not just dangling ones)
docker system prune -a

# Full cleanup, no prompts
docker system prune -a --volumes -f
FlagWhat It Also Removes
(none)Stopped containers, dangling images, unused networks, build cache
-aAll unused images (not just dangling)
--volumesUnused volumes
-fSkips the confirmation prompt

Don't run docker system prune -a --volumes unless you're sure. It removes every container, image, and volume not currently in use, including volumes with data you might still need.

A Practical Cleanup Routine

Daily during active development:

docker container prune -f    # Stopped containers gone
docker builder prune -f      # Build cache cleared

Weekly:

docker image prune -a -f     # Unused images removed
docker volume prune -f       # Orphaned volumes cleared

Quick Reference

CommandWhat It Does
docker system dfShow disk usage breakdown
docker rm -f <c>Force remove a container
docker container pruneRemove all stopped containers
docker run --rmAuto-delete container on exit
docker image prune -aRemove all unused images
docker volume pruneRemove unused volumes
docker builder pruneClear build cache
docker system prune -a --volumesFull cleanup (use carefully)

Key Takeaways

  • docker system df is always your first move as it shows exactly how much space each type of Docker object is using and how much is reclaimable
  • Use --rm on docker run for any container you won't need again.
  • Build cache is always safe to delete with docker builder prune
  • docker volume prune is destructive, data in orphaned volumes is gone permanently; always check docker volume ls first
  • docker system prune -a --volumes is the option used only when you're certain nothing currently unused is worth keeping
  • A regular cleanup routine (daily containers + cache, weekly images + volumes) keeps disk usage manageable before it becomes a problem

Frequently Asked Questions

How do I check how much disk space Docker is using?

Run docker system df. It gives a breakdown by object type (images, containers, volumes, build cache) with a RECLAIMABLE column showing how much you can safely free up.

What's the difference between docker rm and docker container prune?

docker rm removes specific containers you name. docker container prune removes all stopped containers in one go. Use docker rm when you want to be selective; use prune when you want to bulk-clean everything that's no longer running.

Will docker image prune delete images I still need?

docker image prune (without -a) only removes dangling images, untagged layers leftover from builds. It won't touch images that have a tag. Adding -a removes all images not currently used by any container (running or stopped), so if you have images you want to keep but aren't using right now, skip the -a flag.

Is it safe to delete build cache?

Yes, always. Build cache just speeds up future builds. Deleting it means your next build takes longer (a cold build), but it won't break anything. Subsequent builds will recreate the cache layer by layer.

What does --rm do and when should I use it?

--rm tells Docker to automatically delete the container as soon as it exits. Use it for anything you run once and don't need to revisit like one-off commands, scripts, tests, migrations. It's the best way to prevent stopped containers from piling up.

I ran docker volume prune and lost my database data. How do I recover it?

Unfortunately, docker volume prune permanently deletes volume data and there's no built-in recovery. This is why checking docker volume ls before pruning is so important. Going forward, set up regular volume backups.

PreviousPrev
Next

Continue Learning

Networking Basics

Understand Docker's networking model including bridge networks, port publishing, container DNS, and how containers communicate with each other.

10 min·Easy

Volumes & Storage

Persist data beyond container lifecycles with named volumes and bind mounts. Understand when to use each and how to manage them.

9 min·Easy

Env Vars & Config

Pass configuration into containers safely using environment variables, env files, and Docker secrets - without baking secrets into your images.

7 min·Easy

Explore Related Topics

Ku

Kubernetes Tutorials

Orchestrate your Docker containers at scale

CI

CI/CD Tutorials

Automate Docker builds and deployments in pipelines

Try the Tool

Dockerfile Linter

Instantly lint your Dockerfile for best-practice violations and security issues.

On This Page

See What's Taking Up Space FirstRemoving ContainersRemove All Stopped Containers at Once`--rm`Removing ImagesRemoving VolumesRemoving NetworksBuild Cache CleanupThe Nuclear Option: `docker system prune`A Practical Cleanup RoutineQuick ReferenceKey TakeawaysFrequently Asked Questions