Avara
Avara

Reputation: 2103

How to remove all docker volumes?

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?

Upvotes: 182

Views: 181770

Answers (6)

starbeamrainbowlabs
starbeamrainbowlabs

Reputation: 6106

Other answers here do not adequately answer the OP's question. The OP asked how to delete all volumes, not just those not actively being used.

This can be an issue if you e.g. have a complex Docker compose file you're testing that failed and somehow left some containers running without your knowledge.

tldr:

Copy and paste the following commands:

docker stop $(docker ps -aq)
docker container rm -f $(docker container ls -aq)
docker volume rm -f $(docker volume ls -q)

Step 1: Stopping containers

To delete all volumes, one first has to stop all containers. Ref this answer, this can be done like so:

docker stop $(docker ps -aq)

...and verified with this command:

docker ps

...if nothing shows up in the list, then you've stopped all containers.

The reason this is required is because Docker does not allow you to remove volumes for containers that are currently running - even with --force.

Step 2: Deleting all containers

Next, we need to remove all containers:

docker container rm -f $(docker container ls -aq)

Note that containers and images are 2 different things. Containers are instances of a given image with e.g. volumes etc associated with them. Even if you stop containers from running, they are not automatically deleted.

Step 3: Deleting all volumes

Now that all containers are stopped and deleted, we can remove all volumes like so:

docker volume rm -f $(docker volume ls -q)

This works because docker volume ls -q gives a list of all the IDs of all the volumes currently being stored, which is then passed to docker volume rm --force as a space-separated list of volumes to be deleted.

If you have a very large number of docker volumes, you might get an error saying the command line is too long. In this case, we can use the magic of xargs:

docker volume ls -q | xargs docker volume rm -f

...this passes as many docker volume IDs to docker volume rm --force as possible in each invocation.

Step 5: Verification

Finally, you can verify that all volumes have been deleted like so:

docker volume ls

...this should show an empty list with no volumes listed.

Upvotes: 12

VonC
VonC

Reputation: 1324178

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)

OrangeDog adds in the comments:

The prune now only removes "anonymous" volumes.
You'll need the older solution to get rid of all the dangling ones.

True.

In Docker, a "dangling" volume refers to a volume that is no longer associated with a container. That terminology is more frequently associated with images, where "dangling" images are those which are not tagged and are not referenced by any container.

To remove dangling volumes, you can use the following command:

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

(also mentioned in this thread)

Upvotes: 366

Wajid Shaikh
Wajid Shaikh

Reputation: 439

if you want to remove specific voulume to delete with * for e.g. runner-*

do ls first

with command docker volume ls --format '{{ .Name }}' | grep -E "^runner-*"

if you want to change the string just replace name of runner with ur volume name

then just merge with remove volume command

docker volume rm $(docker volume ls --format '{{ .Name }}' | grep -E "^runner-*")

Upvotes: 0

Jonathan Lin
Jonathan Lin

Reputation: 20694

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

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

Upvotes: 15

vvchik
vvchik

Reputation: 1455

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

Upvotes: 58

Marc Young
Marc Young

Reputation: 4012

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

Upvotes: 11

Related Questions