Difficulty: Intermediate
How do you remove a container together with its anonymous volumes, and why does Docker not do this by default?
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.
-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.-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.
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.
| Command | What it removes |
|---|---|
docker rm -v web | Container web and its anonymous volumes |
docker run --rm ... | Container and its anonymous volumes when it exits |
docker volume rm pgdata | One named volume (fails if in use) |
docker volume prune | Unused anonymous volumes (default since Engine 23.0) |
docker volume prune -a | All unused volumes, named included |
docker compose down -v | The 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.
-v means different things. On docker run, -v mounts a volume; on docker rm, -v removes volumes. Same letter, unrelated meaning.--volumes-from), it is not removed.docker compose down -v on a developer machine is handy; in a shared staging environment it deletes the database. Know which you are running.docker volume ls -f dangling=true lists volumes not referenced by any 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.
docker rm -v, Anonymous volumes, Named volumes, --rm, docker volume prune