Docker & orphaned volumes

What is the best way to get rid of orphaned Docker volumes?

I have written one liner that seems to be solving this problem. First lets check how many volumes there are in total:


root@quad:~# docker volume ls | wc -l
72

72 volumes. Quite a lot to be honest.

Then I run this to double check that it really does return valid data.


for vol in `docker volume ls -qf dangling=true`; do  docker volume inspect $vol | jq -r '.[] | .Mountpoint' | xargs ls ; done

and I get 52 lines like these:


ls: cannot access '/opt/docker/volumes/07a1d0711833af526d08809118ae880fce4d6c537d4246c2fc332fceca0cde9a/_data': No such file or directory
ls: cannot access '/opt/docker/volumes/0d88bab60c8f5023a22f3ff15b35ab585c66b476f2e6615db06f50fefb2bc970/_data': No such file or directory
ls: cannot access '/opt/docker/volumes/0e4e14e289b25a65d0ee597bc7810c6f09961d13a059293a357c425b46534f5a/_data': No such file or directory
ls: cannot access '/opt/docker/volumes/18c2481850150cab1c46b0bdc9178e959c05dd4a205a45510c20467c6b95e473/_data': No such file or directory
ls: cannot access '/opt/docker/volumes/2275d260b670d10d026f1be776a9d32b9a26e23d513cca63add039e9451e3454/_data': No such file or directory
ls: cannot access '/opt/docker/volumes/28252f4885f02615e33d1ed2fdc4b45ebb21d38b933457760090f9f3b68f4e09/_data': No such file or directory

which means that only 20 volumes are actually valid, rest of them point to nonexistent folders on the hard drive.

So, they can be easily removed by running


docker volume ls -qf dangling=true | | xargs -r docker volume rm

Leave a Reply

Your email address will not be published. Required fields are marked *