154

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?

5 Answers 5

309
+50

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)

4
  • 3
    The prune commands are great, but do be careful with them. It seems like they assume that containers are running at the time you call them. It'll purge things that might be associated but not actively in use (e.g. networks) leading to failures next time existing containers start. Use the dangling filters ahead of time if you're unsure of what might be pruned :)
    – jocull
    Jul 25, 2018 at 14:24
  • 7
    docker system prune doesn't seem to remove local volumes but docker volume prune sorts it out
    – Maciek Rek
    Jul 6, 2020 at 15:46
  • The prune now only removes "anonymous" volumes. You'll need the older solution to get rid of all the dangling ones.
    – OrangeDog
    Oct 24 at 13:33
  • @OrangeDog I agree. I have edited the answer to address your comment.
    – VonC
    Oct 24 at 19:49
47

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

4
  • 1
    I wouldn't run it with without testing the list of dangling volumes with --dry-run parameter first. Apr 16, 2016 at 20:57
  • There is no --dry-run option. Nov 23, 2016 at 13:23
  • 3
    this answer is the one if you want to delete all volumes no matter of what. thanks! Nov 6, 2019 at 14:57
  • This is answer is NOT OUTDATED: I'm on Docker 24.0.4, and docker volume prune -f left over 1,300 volumes untouched on my Ubuntu. I tried many things, but eventually only docker volume rm $(docker volume ls -qf dangling=true) removed them.
    – not2savvy
    Jul 17 at 14:06
12

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

$ docker volume rm $(docker volume ls -qf dangling=true | xargs)
2
  • 1
    docker volume prune is useless and does nothing. This command really removes all volumes as the question asks.
    – Nairum
    Feb 5 at 16:23
  • docker volume prune does not delete dangling volumes, but it isn't uselss, its the same with docker container prune Apr 13 at 16:26
8

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
0
0

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-*")

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.