Skip to content

Instantly share code, notes, and snippets.

@marcelotmelo
Last active June 21, 2016 13:56
Show Gist options
  • Save marcelotmelo/3681fc53e4bc0250a75f78b13370e87c to your computer and use it in GitHub Desktop.
Save marcelotmelo/3681fc53e4bc0250a75f78b13370e87c to your computer and use it in GitHub Desktop.
#list all volumes
docker volume ls | awk '{if(NR>1) print $2}' | sort > all_volumes
#list currently used volumes
docker ps -a | awk '{if(NR>1)print $1}' | xargs docker inspect | tr -d '\n' | grep -Po '"Mounts":.*?[^\\]",' | awk '/Name/ {print $5}' | tr -d '"' | tr -d ',' > used_volumes
#remove unused volumes
diff used_volumes all_volumes --suppress-common-lines | grep '>' | awk '{print $2}' | xargs docker volume rm
@marcelotmelo
Copy link
Author

Snippets for listing docker volumes
  • The first command will create a file named "all_volumes" with all the volumes hashes, each in one line, sorted
  • The second command will create a file named "used_volumes" with the volumes used by the current docker machines, sorted
  • The third command will remove the unused volumes by making a diff between the files

You should not do this unless you're sure about what you're doing. You will lose the data of you containers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment