Skip to content

Instantly share code, notes, and snippets.

@normoes
Last active November 23, 2020 08:10
Show Gist options
  • Save normoes/37ce1ddc989630cc8ecb5852c4340979 to your computer and use it in GitHub Desktop.
Save normoes/37ce1ddc989630cc8ecb5852c4340979 to your computer and use it in GitHub Desktop.
Remove docker volumes

This is more or less a sequence of manual steps, because I don't want to remove volumes I might still need, especially if no containers are using those volumes at the moment.

This is also why I, sometimes, prefer the following over: docker volume prune.


In situations where I have literally thousands of docker volumes, basically after a long time of not removing any docker volumes, the following comes in handy.

At first I get the 200 (just some number) biggest docker volumes (as root). I exclude containers, that contain postgres or redis in their names, essentially allowing the removal of all other volatile containers, named and unnamed.

# Just to show the format:
du -s /var/lib/docker/volumes/* | sort -k1 -rn | head -n 200 | grep -i -vE "postgres|redis"
# Output:
# 40120	/var/lib/docker/volumes/0e7aa18c73bec2379bef2adc077990d714c547c7b1719c95057db3443bb00d58
# 40120	/var/lib/docker/volumes/0e33840746b64e945c05730d8114b1097053464456a92806780a9eef57296f7e
# 40120	/var/lib/docker/volumes/0d9929b8d1b56a3b128df965c9fb42c4a6948cc058edfbb49b7f027bc3539ae8

# The actual command only returns the volume names:
du -s /var/lib/docker/volumes/* | sort -k1 -rn | head -n 200 | grep -i -vE "postgres|redis" | awk -F "/" '{print $NF}
# Output:
0e7aa18c73bec2379bef2adc077990d714c547c7b1719c95057db3443bb00d58
0e33840746b64e945c05730d8114b1097053464456a92806780a9eef57296f7e
0d9929b8d1b56a3b128df965c9fb42c4a6948cc058edfbb49b7f027bc3539ae8

Then, after checking the result of the above command, I remove the docker volumes:

docker volume rm -f $(du -s /var/lib/docker/volumes/* | sort -k1 -rn | head -n 200 | grep -i -vE "postgres|redis" | awk -F "/" '{print $NF}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment