Cleanup Commands

Difficulty: Beginner

Question

How do you clean up unused Docker images, containers, networks, volumes and build cache, and what exactly does each prune command delete?

Answer

Use docker system prune to remove stopped containers, unused networks, dangling images and unused build cache in one go. Add -a to also remove every image not used by any container, and --volumes to include unused volumes. For finer control, use the per-object commands (docker container prune, docker image prune, docker volume prune, docker builder prune) and check space first with docker system df.

Measure before you delete

docker system df shows how much space images, containers, local volumes and build cache use, and how much of that is reclaimable. docker system df -v breaks it down per image and per volume. On a CI runner or a small cloud VM, build cache is very often the biggest item, not images.

What each command removes

CommandRemoves
docker container pruneAll stopped containers
docker image pruneDangling images only (untagged <none>:<none> left behind by rebuilds)
docker image prune -aEvery image not used by at least one container
docker network pruneCustom networks not used by any container
docker volume pruneUnused anonymous volumes (add -a for unused named volumes too)
docker builder pruneDangling build cache (-a for all build cache)
docker system pruneStopped containers, unused networks, dangling images, unused build cache
docker system prune -a --volumesAll of the above plus unused images and unused volumes

Two details people get wrong:

  • Volumes are never pruned by default by docker system prune. This is deliberate: volumes hold data such as a Postgres database.
  • Since Docker Engine 23.0, docker volume prune only removes anonymous volumes by default. Named volumes like pgdata need docker volume prune -a. Older tutorials that say otherwise predate this change.

"Unused" means not referenced by any container, including stopped ones. A stopped container still holds its image and volumes, which is why pruning containers first frees more on the next step.

Pruning safely with filters

  • --filter "until=24h" only removes objects created more than 24 hours ago, which avoids deleting an image a teammate just built.
  • --filter "label=env=ci" or label!=keep targets objects by label.
  • -f / --force skips the confirmation prompt, which you need in cron jobs and CI.

A typical nightly CI cleanup is docker system prune -af --filter "until=72h" plus docker builder prune -af --filter "until=72h". Avoid --volumes on any server that runs stateful containers.

Mistakes to avoid

  • Running docker system prune -a --volumes on a dev laptop to "fix" disk space and losing your local database volume.
  • Deleting things by hand under /var/lib/docker. Docker tracks metadata separately; always clean up through the CLI.
  • Forgetting logs. Container JSON logs live with the container and can grow huge; set max-size and max-file in the log options instead of pruning around the problem.

Code examples

Inspect usage, then prune selectively

# How much space is used and reclaimable?
docker system df

# Stopped containers and dangling images older than a day
docker container prune -f --filter "until=24h"
docker image prune -f --filter "until=24h"

# Build cache usually frees the most on CI runners
docker builder prune -af --filter "until=72h"

# Unused named volumes too (careful: this deletes data)
docker volume prune -a

Check what is reclaimable first, then prune by type with an age filter so recent work is left alone.

Capping container log growth

{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}

Put this in /etc/docker/daemon.json and restart the daemon; it applies to newly created containers and stops logs from silently filling the disk.

Key points

  • docker system df shows what is using space and how much is reclaimable.
  • docker system prune never touches volumes unless you pass --volumes.
  • Since Engine 23.0, docker volume prune removes only anonymous volumes unless you add -a.
  • image prune removes dangling images; image prune -a removes every image without a container.
  • Use --filter "until=..." and -f for safe, automated cleanup in CI.

Common follow-up questions

  • What is a dangling image and how is it created?
  • How would you automate cleanup on a CI runner without breaking parallel jobs?
  • Why does a stopped container prevent its image from being pruned?
  • How do you limit the size of container logs?

Interview tips

  • Mention docker system df first; measuring before deleting sounds like an operator, not a guesser.
  • Call out that volumes are excluded by default and why, since that is where data loss happens.
  • Knowing the Engine 23.0 change to volume prune shows you keep up with current behaviour.

Concepts covered

docker system prune, Dangling images, Build cache, docker system df, Prune filters

More Docker & Containers questions