Pruning Volumes

Difficulty: Beginner

Question

How do you remove unused Docker volumes, and how do you make sure you do not delete data you still need?

Answer

Run docker volume prune to remove unused volumes, meaning volumes not referenced by any container, running or stopped. Since Docker Engine 23.0 it only removes unused anonymous volumes by default; add -a (--all) to also remove unused named volumes. Always list what will go first with docker volume ls -f dangling=true, because pruned volumes and their data cannot be recovered.

Anonymous versus named volumes

  • Anonymous volumes have a random 64-character name. They are created by a VOLUME instruction in an image (the official postgres and mysql images declare one) or by -v /data without a name. They pile up quietly as containers are recreated.
  • Named volumes are created explicitly (docker volume create pgdata, -v pgdata:/var/lib/postgresql/data, or a top-level volumes: key in Compose). These normally hold data you care about.

The Engine 23.0 change made the default safer: a plain docker volume prune will no longer delete your named database volume just because its container was removed. Many older blog posts describe the previous behaviour.

What counts as "unused"

A volume is in use if any container references it, including stopped containers. So:

  1. A stopped Postgres container still protects its volume.
  2. After docker rm of that container, the volume becomes dangling and eligible for pruning.
  3. docker container prune followed by docker volume prune -a can therefore wipe a database you only stopped temporarily.

Safer cleanup habits

  • Preview first: docker volume ls -f dangling=true shows exactly which volumes are unused.
  • Filter by label: docker volume prune -a --filter "label!=keep" skips volumes you labelled with keep, and --filter label=env=ci limits pruning to CI volumes.
  • Remove anonymous volumes with their container: docker run --rm removes the container's anonymous volumes on exit, and docker rm -v does the same when removing a container manually.
  • Compose: docker compose down keeps volumes; docker compose down -v removes the project's named and anonymous volumes. Use -v only when you really want a clean database.
  • Back up before risky cleanup: run a throwaway container that mounts the volume and tars it to a bind-mounted folder.
  • Use -f to skip the confirmation prompt in scripts, and only in scripts you trust.

docker system prune --volumes also prunes volumes as part of a wider cleanup, which is convenient on CI runners but a bad habit on any machine that holds real data.

Code examples

Preview, protect and prune volumes

# What is unused right now?
docker volume ls -f dangling=true

# Remove unused anonymous volumes only (default since Engine 23.0)
docker volume prune -f

# Include named volumes, but skip anything labelled keep
docker volume prune -a -f --filter "label!=keep"

# Protect an important volume by labelling it at creation
docker volume create --label keep=true pgdata

Listing dangling volumes first and protecting important ones with a label makes pruning predictable.

Back up a named volume before cleanup

# Tar the contents of pgdata into the current directory
docker run --rm \
  -v pgdata:/source:ro \
  -v "$PWD":/backup \
  alpine tar czf /backup/pgdata-$(date +%F).tar.gz -C /source .

A short-lived container mounts the volume read-only and writes a compressed archive to the host. For a live database, prefer a logical dump such as pg_dump.

Key points

  • docker volume prune removes volumes not used by any container, running or stopped.
  • Since Engine 23.0 the default is anonymous volumes only; -a includes named volumes.
  • Preview with docker volume ls -f dangling=true before pruning.
  • docker run --rm and docker rm -v clean up anonymous volumes with the container.
  • docker compose down -v deletes the project's volumes; plain down keeps them.

Common follow-up questions

  • What is the difference between a named volume and a bind mount?
  • How would you back up and restore a Docker volume?
  • Why do anonymous volumes accumulate over time?
  • What does docker compose down -v remove?

Interview tips

  • Mention the Engine 23.0 default change; it separates current knowledge from old tutorials.
  • Talk about safety (preview, labels, backups) and not just the command itself.
  • Explain that stopped containers still hold their volumes; it shows you understand the reference rule.

Concepts covered

docker volume prune, Anonymous volumes, Named volumes, Dangling volumes, Data safety

More Docker & Containers questions