Skip to content

Instantly share code, notes, and snippets.

@fvztdk
Created October 4, 2017 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvztdk/18288a8a3d6726dec7a1f9599948c275 to your computer and use it in GitHub Desktop.
Save fvztdk/18288a8a3d6726dec7a1f9599948c275 to your computer and use it in GitHub Desktop.
Clear older than X weeks docker images
#!/bin/bash
#use this to remove older docker images left on CI machine, max_week_size represent the number of weeks that should be kept
max_week_size=4
docker images | awk 'NR>1 {print $0}' | while read line; do
# echo $line
id_img=$(echo $line | awk '{print $3}')
# if older then a month
is_month=$(echo $line | grep 'month')
if [ ! -z "$is_month" ]; then
echo $id_img
docker rmi -f $id_img
continue
fi
# remove older then 4 weeks
num_week=$(echo $line | grep "week" | awk '{print $4}')
if [ ! -z "$num_week" ] && [ $num_week -ge $max_week_size ]; then
echo $id_img
docker rmi -f $id_img
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment