Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all docker volumes?

Tags:

docker

If I do a docker volume ls, my list of volumes is like this:

DRIVER              VOLUME NAME
local               305eda2bfd9618266093921031e6e341cf3811f2ad2b75dd7af5376d037a566a
local               226197f60c92df08a7a5643f5e94b37947c56bdd4b532d4ee10d4cf21b27b319
...
...
local               209efa69f1679224ab6b2e7dc0d9ec204e3628a1635fa3410c44a4af3056c301

and I want to remove all of my volumes at once. How can I do it?

like image 906
Avara Avatar asked Apr 16 '16 11:04

Avara


People also ask

Can I delete all docker volumes?

The prune command allows you to delete unused Docker objects (containers, images, networks, volumes) all at once. The following is an example. At this time, by using the —filter option, it is possible to narrow down and delete the containers that have been stopped for a long time.

How do I clean up docker volumes?

Prune everything The docker system prune command is a shortcut that prunes images, containers, and networks. Volumes are not pruned by default, and you must specify the --volumes flag for docker system prune to prune volumes. By default, you are prompted to continue. To bypass the prompt, use the -f or --force flag.

Should I delete docker volumes?

Given you likely deleted the container long ago, the volumes are almost always safe to delete. You can run the following to delete anything with the long hash name. The deletes will fail if the volumes are currently in use, so there's no risk to running or even stopped containers.


4 Answers

The official command to remove all unused data (including volumes without containers) will be with docker 1.13

docker system prune  

If you want to limit to volumes alone, removing only unused volumes:

docker volume prune

You also have docker image prune, docker container prune, etc:
See more at "Prune unused Docker objects".

See commit 86de7c0 and PR 26108.

You can see it in action in play-with-docker.com:

/ # docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
1296a5e47ef3        hello-world         "/hello"            7 seconds ago       Exited (0) 6 seconds ago                       prickly_poincare

/ # docker system  prune
WARNING! This will remove:
        - all stopped containers
        - all volumes not used by at least one container
        - all networks not used by at least one container
        - all dangling images
Are you sure you want to continue? [y/N] y
Deleted Containers:
1296a5e47ef3ab021458c92ad711ad03c7f19dc52f0e353f56f062201aa03a35

The current (pre-docker 1.13) way of managing volume was introduced with PR 14242 and the docker volume command, which documents in its comment from July 2015:

docker volume rm $(docker volume ls -q --filter dangling=true)
like image 192
VonC Avatar answered Dec 06 '22 12:12

VonC


Edited on 2017: This answer was given on Apr 16 '16 and now is outdated, and correct only for docker version prior to 1.13 please use the answer from @VonC, now it is marked as correct

To delete unused volumes you can use the built-in docker volume rm command. The rm command also deletes any directory in /var/lib/docker/volumes that is not a volume, so make sure you didn't put anything in there you want to save:
Command to List volumes, little bit right than yours:

$ docker volume ls -qf dangling=true

Cleanup:

$ docker volume rm $(docker volume ls -qf dangling=true)

more details about ls here, about rm here

like image 32
vvchik Avatar answered Dec 06 '22 13:12

vvchik


This is what I've found to be useful: https://github.com/chadoe/docker-cleanup-volumes

Shellscript to delete orphaned docker volumes in /var/lib/docker/volumes and /var/lib/docker/vfs/dir Docker version 1.4.1 up to 1.11.x

It basically does a cleanup of any orphaned/dangling volumes, but it includes a --dry-run but it makes note of some docker included commands as well (which are referenced in prev comment)

Note about Docker 1.9 and up

To delete orphaned volumes in Docker 1.9 and up you can also use the built-in docker volume commands instead of this docker-cleanup-volumes script. The built-in command also deletes any directory in /var/lib/docker/volumes that is not a volume so make sure you didn't put anything in there you want to save:

List:

$ docker volume ls -qf dangling=true

Cleanup:

$ docker volume rm $(docker volume ls -qf dangling=true)

Or, handling a no-op better but Linux specific:

$ docker volume ls -qf dangling=true | xargs -r docker volume rm
like image 21
Marc Young Avatar answered Dec 06 '22 13:12

Marc Young


To answer the question and borrowing from Marc, this works:

$ docker volume rm $(docker volume ls -qf dangling=true | xargs)
like image 31
Jonathan Lin Avatar answered Dec 06 '22 13:12

Jonathan Lin