Search code examples
docker

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?


Solution

  • 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)