Docker Community Forums

Share and learn in the Docker community.

Simple script needed to delete all docker images over 4 weeks old

I know similar questions have been asked before, but I cannot find anything that does exactly what I need. I don’t have access to the new ‘docker image prune’ command, as I cannot upgrade from my current version 1.11.2 to the latest whizzy 1.13 yet, but I need a simple script that can be run under cron to remove all images, whether dangling or not, over 1 month old.

If docker images reported “4 weeks” as “1 month” it would be simple, I could use something like

docker images | grep " [months]* " 

and pipe the output to the docker rmi command. However, since it appears to report in “weeks” up to 11 weeks, and only “months” once the image is 3 months old or over, I can’t do this.

docker images | grep " [months|weeks]* ago" | awk '{print $3,$4,$5}'

will give me this output::

6828f152f9cc 2 weeks
52a7412befd3 2 weeks
04c2b29e5e08 2 weeks
cdfb37d22663 2 weeks
ca38a8cabb2b 3 weeks
8b043f2395ba 3 weeks
2aa8b20380a0 3 weeks
1ac7dcf35935 9 weeks
f80873c4845b 9 weeks
d5700d37ee95 9 weeks
594dc21de8de 9 weeks
0c9dacb7fa7c 10 weeks
11f3eec60e17 3 months
b13ebf1a4999 3 months
68b0d6a3be3b 7 months
baadc9c8b0ce 8 months
10eb0d6b2b99 9 months

… but I’m stuck trying to figure out how to test $4,$5 to only include anything that’s either “months” or >= “4 weeks” which can then be piped through to docker rmi . Anyone any clues for a simple bit of code that can do this, please?

could also check out this:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -e GRACE_PERIOD_SECONDS=2419200 -e FORCE_IMAGE_REMOVAL=1 -v /etc:/etc:ro spotify/docker-gc

Thanks but I’ve looked into docker-gc and it doesn’t provide the functionality I need, unfortunately.

could try https://github.com/docker/docker-py
there you can easily list all images and do determine the age in python.
if you do it in bash/awk i doubt you could still understand your script in 3 months. :slight_smile:

I suspect you’re right :slight_smile: except that I can’t code in python yet …

Bump … anyone able to lend a hand with this script, please?

docker system prune and docker image prune have the until filter. So docker image prune --all --filter until=48h would remove all (not just dangling) images that were created more than 48 hours ago. Hopefully that helps.

2 Likes

list=$(docker images | grep " [hours]* ago" | awk ‘{print $3}’)
for value in $list
do
echo “deleting $value”
docker rmi $value
done

1 Like

Yup, worked for me. I tried 60d, but it didn’t like that specifier, but this worked:
docker image prune --all --filter until=5m

(5 months)

I’m pretty sure 5m is five minutes, not five months. I can’t find how to do a month, so I did it with 720 hours.

docker image prune --all --filter "until=720h"

here is a script, and you can write it in your .bashrc

#!/bin/bash

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