Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Clean old Docker Images
# Keeps latest X tags of every Docker Image
echo Retrieving local images available
# get all images
IMAGES=$(docker images | sort | awk '{print $1}' | uniq | grep -v "<none>")
echo Cleaning old tags...
for i in $IMAGES; do
COUNT=$(docker images | grep "$i " | wc -l)
if [[ $COUNT -ge 3 ]]; then
echo IMAGE $i has $COUNT tags
docker images | grep "$i " | tail -$(($COUNT-$KEEP_IMAGES_BACK)) | awk '{print $1 ":" $2}' | xargs docker rmi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment