Last active
March 15, 2018 17:09
-
-
Save iMerica/0045a6a814ad4d9c8a264f046fc44337 to your computer and use it in GitHub Desktop.
Delete Docker Images that are older than N days
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
Author
iMerica
commented
Mar 15, 2018
Use docker image prune
instead:
https://docs.docker.com/engine/reference/commandline/image_prune/#description
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment