Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to delete exited containers and untagged/unused images from docker
set -o errexit
echo "Removing exited docker containers..."
docker ps -a -f status=exited -q | xargs -r docker rm -v
echo "Removing dangling images..."
docker images --no-trunc -q -f dangling=true | xargs -r docker rmi
echo "Removing unused docker images"
images=($(docker images --digests | tail -n +2 | awk '{ img_id=$1; if($2!="<none>")img_id=img_id":"$2; if($3!="<none>") img_id=img_id"@"$3; print img_id}'))
containers=($(docker ps -a | tail -n +2 | awk '{print $2}'))
containers_reg=" ${containers[*]} "
for item in ${images[@]}; do
if [[ ! $containers_reg =~ " $item " ]]; then
remove_images=" ${remove[*]} "
echo ${remove_images} | xargs -r docker rmi
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.