Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleanup docker disk space https://lebkowski.name/docker-volumes/
#!/bin/bash
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'
) | xargs -r rm -fr
@danstreeter

This comment has been minimized.

danstreeter commented Oct 31, 2016

Hey - Found this on your site and it really helped me out - thanks, however I think you can deal with the unused volumes with inbuilt commands instead of drilling through your filesystem with a complex command:

docker volume ls -f dangling=true | awk '{ print $2 }' | xargs docker volume rm

@mlebkowski

This comment has been minimized.

Owner

mlebkowski commented Dec 7, 2016

@danstreeter, yep, this is just a part of the blog post where I mentioned the solution you posted. This is, however, only for docker version 1.9 and later

@Shuliyey

This comment has been minimized.

Shuliyey commented Jan 23, 2017

thanks for the script, it's great 😄

I've updated the remove unused images part a bit

#!/bin/bash

# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v

# remove unused images:
local images=`docker images --no-trunc`;
local lines=$(echo "$images" | awk '{print $2}' | grep -n "<none>" | cut -d: -f1 | sed 's/$/p/g');
lines=`echo $lines`;
lines=${lines// /;};
local image_ids=$(echo "$images" | awk '{print $3}' | sed -n "$lines");
[[ -n "${image_ids[@]}" ]] && docker rmi ${image_ids[@]}

# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
  docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'
) | xargs -r rm -fr
@zaplatynski

This comment has been minimized.

zaplatynski commented Jan 5, 2018

Since the latest docker version there are special prune commands:

#!/bin/bash
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused containers:
yes | docker container prune
# remove unused images:
yes | docker image prune
# remove unused volumes:
yes | docker volume prune
@jc00ke

This comment has been minimized.

jc00ke commented Jul 16, 2018

@zaplatynski 👏 🎉

@givankin

This comment has been minimized.

givankin commented Aug 13, 2018

As of today, all prune commands were merged into single docker system prune: https://docs.docker.com/engine/reference/commandline/system_prune/#examples

@Qix-

This comment has been minimized.

Qix- commented Oct 21, 2018

Which is a terrible design choice because it unknowingly purges just about everything useful to use Docker by default, which causes a ton of development overhead helping colleagues recover network configuration, etc. when simply trying to free up space on the system.

Docker seems to mess up just about everything they do.

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