Removing Volumes with Containers

Difficulty: Intermediate

Question

How do you remove a container together with its anonymous volumes, and why does Docker not do this by default?

Answer

Use docker rm -v <container> (add -f if it is still running). The -v flag removes the container and any anonymous volumes attached to it in the same step. Named volumes are never deleted by docker rm -v, because Docker assumes a volume you named on purpose holds data you want to keep.

Anonymous versus named volumes

  • Anonymous volume: created without a name, either by -v /data on the command line or by a VOLUME /var/lib/mysql instruction in the image's Dockerfile. Docker gives it a random 64-character ID.
  • Named volume: created with -v pgdata:/var/lib/postgresql/data or docker volume create pgdata. You can reattach it to a new container by name.

Many official database images (postgres, mysql, mongo) declare VOLUME, so every docker run postgres without -v silently creates an anonymous volume. Remove those containers with plain docker rm and the volumes stay behind, slowly filling the disk.

Why removal is opt-in

Volumes exist to outlive containers. If docker rm deleted volumes by default, a routine "remove and recreate to upgrade the image" would wipe a database. So Docker makes data deletion explicit: -v for anonymous volumes on rm, and a separate docker volume rm for named ones.

Ways to clean up volumes

CommandWhat it removes
docker rm -v webContainer web and its anonymous volumes
docker run --rm ...Container and its anonymous volumes when it exits
docker volume rm pgdataOne named volume (fails if in use)
docker volume pruneUnused anonymous volumes (default since Engine 23.0)
docker volume prune -aAll unused volumes, named included
docker compose down -vThe project's containers, networks, named volumes declared in the file, and anonymous volumes

The docker volume prune change in Docker Engine 23.0 surprises people reading older blog posts: it used to delete unused named volumes too, and now needs -a (--all) for that.

Gotchas

  • -v means different things. On docker run, -v mounts a volume; on docker rm, -v removes volumes. Same letter, unrelated meaning.
  • Shared anonymous volume. If another container still uses the volume (for example via --volumes-from), it is not removed.
  • Compose and data loss. docker compose down -v on a developer machine is handy; in a shared staging environment it deletes the database. Know which you are running.
  • Finding leftovers. docker volume ls -f dangling=true lists volumes not referenced by any container.

Code examples

See the anonymous volume, then remove it with the container

# postgres image declares VOLUME, so this creates an anonymous volume
docker run -d --name tmp-db -e POSTGRES_PASSWORD=dev postgres:16

docker inspect tmp-db --format '{{range .Mounts}}{{.Type}} {{.Name}} -> {{.Destination}}{{end}}'
# volume 3f9c1e...(64 hex chars) -> /var/lib/postgresql/data

# Remove the running container and its anonymous volume in one step
docker rm -fv tmp-db

# Find any older leftovers that nothing references
docker volume ls -f dangling=true
docker volume prune -f   # anonymous only; add -a for named volumes too

docker inspect shows the random volume name, which is how you recognise an anonymous volume. -fv combines force (stop if running) and volume removal.

Key points

  • docker rm -v removes anonymous volumes only, never named volumes
  • VOLUME in a Dockerfile creates an anonymous volume for every container that does not mount one there
  • docker run --rm also removes anonymous volumes on exit
  • Since Engine 23.0, docker volume prune removes only anonymous volumes unless you pass -a
  • docker compose down -v also deletes named volumes declared in the compose file

Common follow-up questions

  • Why do volumes keep accumulating on a CI runner, and how would you stop it?
  • What is the difference between a volume and a bind mount?
  • How would you back up a named volume before deleting it?

Interview tips

  • Draw the anonymous versus named distinction first; the rest of the answer depends on it
  • Explain the design reason (data safety) instead of just stating the flag
  • Mention the Engine 23.0 change to volume prune to show you are current

Concepts covered

docker rm -v, Anonymous volumes, Named volumes, --rm, docker volume prune

More Docker & Containers questions