Skip to content

Instantly share code, notes, and snippets.

@timfeirg
Created June 7, 2018 15:48
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 timfeirg/9e8eda2921c382a9096a98c394b7fb87 to your computer and use it in GitHub Desktop.
Save timfeirg/9e8eda2921c382a9096a98c394b7fb87 to your computer and use it in GitHub Desktop.
delete old docker images
#!/bin/env bash
IFS='
'
long_ago=`date +%Y-%m-%d --date='2 weeks ago'`
for image in $(docker images --format '{{.CreatedAt}}|{{.Repository}}:{{.Tag}}')
do
created=`echo ${image%|*} | grep -oP "^[^\s]+"`
tag=${image#*|}
if [ -z "$created" ] || [ -z "$tag" ]; then
continue
fi
if [[ $created > $long_ago ]]; then
echo "delete old image: $image"
docker rmi $tag || true
fi
done
@timfeirg
Copy link
Author

timfeirg commented Jun 7, 2018

docker prune images doesn't seem to support the until=24h filter, so I created this shell script to do roughly the same job.

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