Skip to content

Instantly share code, notes, and snippets.

@iMerica
Last active March 15, 2018 17:09
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 iMerica/0045a6a814ad4d9c8a264f046fc44337 to your computer and use it in GitHub Desktop.
Save iMerica/0045a6a814ad4d9c8a264f046fc44337 to your computer and use it in GitHub Desktop.
Delete Docker Images that are older than N days
# Example: `remove_old_images 30` // Deletes all Docker Images 30 days or older
function remove_old_images() {
if [ $# -eq 0 ]
then
echo "Pass in the number of days"
exit 0
fi
docker images --format '{{.ID}} {{.CreatedAt}}' | \
awk '{print $1 " " $2 }' | \
python -c "import fileinput,datetime; print('\n'.join([i.rstrip().split(' ')[0] for i in fileinput.input() if (datetime.datetime.strptime(i.rstrip().split(' ')[1], '%Y-%M-%d') - datetime.datetime.now()).seconds/3600 > int($1)]))" \
xargs docker rmi
}
@iMerica
Copy link
Author

iMerica commented Mar 15, 2018

⚠️ DO NOT RUN THIS UNLESS YOU KNOW WHAT YOU ARE DOING ⚠️

@iMerica
Copy link
Author

iMerica commented Mar 15, 2018

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