Sub topic 7
Pruning volumes without losing data
This chapter assumes you can run docker run, docker ps and docker volume ls. It also assumes you know that containers are disposable and that volumes hold data that outlives them.
Why volumes are different
Images, stopped containers and build cache can be rebuilt or re-pulled. A volume can hold a database, uploads or other state that exists nowhere else. Deleting a volume deletes that data, and Docker has no undo.
For this reason, docker system prune skips volumes unless you add --volumes. Treat volume cleanup as a separate, deliberate step.
What counts as unused
A volume is unused when no container references it. Stopped containers count as references, so a volume attached to a stopped container is not unused. Remove that container, and its volume becomes unused.
List the unused volumes first:
docker volume ls --filter dangling=trueIn this command, "dangling" means "not referenced by any container". It does not mean the same thing as a dangling image.
Next, check how much space volumes use:
docker system df -vThe "Local Volumes space usage" section shows each volume's size and its link count. A link count of 0 means no container uses it.
Running the prune
docker volume pruneDocker asks for confirmation, then removes the unused volumes and reports the space reclaimed.
Anonymous and named volumes
Behavior depends on your Docker Engine version:
- Docker Engine 23.0 and later:
docker volume pruneremoves only anonymous volumes (the ones with random hash names). To include named volumes, add-aor--all. - Earlier versions: the command removes every unused volume, named or not.
Check with docker version before relying on either behavior. If you are unsure, run docker volume ls --filter dangling=true and compare the list with what you expect to lose.
Back up before you delete
If a volume might matter, copy its contents out first. This command mounts the volume read-only and writes a compressed archive to the current directory:
docker run --rm -v mydata:/data:ro -v "$PWD":/backup alpine \
tar czf /backup/mydata.tgz -C /data .The --rm flag removes the temporary container when it exits. Replace mydata with your volume name.
The decision flow
Pruning with the system command
To remove volumes together with everything else unused, opt in explicitly:
docker volume prune
docker system prune --volumesThe second command also deletes stopped containers, unused networks, dangling images and build cache. Run the volume check first, since it is the only part that can destroy data.
You can also remove one volume by name. This fails safely if a container still uses it:
docker volume rm mydataCommon errors
- Assuming a stopped database is safe. Its volume is still referenced, so prune will not touch it. Remove the container and the volume becomes eligible.
- Pruning on a shared host. Another team's stopped project may own volumes you cannot see the purpose of.
- Scheduling
--volumes. Automated cleanup should leave volumes out, so a forgotten project cannot lose its data silently.
Exercise
You run docker volume ls --filter dangling=true and see a volume named pgdata. You are not sure what it belongs to. What should you do before running docker volume prune -a, and why?
Show answer
Back it up first, for example with a temporary container that runs tar on the mounted volume, and confirm who owns it. A volume listed as dangling has no container referencing it, but it can still hold data you need, such as a database from a project whose container was removed. Pruning deletes it permanently.