Simple script needed to delete all docker images older than 4 weeks

docker image prune
docker remove old images
docker remove all images
docker images filter
docker-compose build remove old image
docker intermediate images
docker clear cache
docker-remove dead images

I know similar questions have been asked before, but I cannot find anything that does exactly what I need. I've also asked on the Docker Community forums, but nobody could help.

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. I don't need to worry about containers, as this is from a repo that only stores images.

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?

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
done

Simple script needed to delete all docker images over 4 weeks old , Simple script needed to delete all docker images over 4 weeks old. 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. 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

Just awkneeded:

docker images |awk '$5=="months" || ($4>4 && $5=="weeks") {print $3,$4,$5}'

Explanation

If 5th field is "months", or equals to "weeks" and 4th greater than 4, print the required columns.

How To Remove Docker Containers, Images, Volumes, and , Is there a shortcut rmi command to delete all images that are Will list all docker images and then docker rmi them. 4 Likes It would be super nice if you could just docker rm all the containers no longer in use and then, docker rmi --unused A simple Docker container and image garbage collection script. Clean all Docker images older than 4 weeks. GitHub Gist: instantly share code, notes, and snippets.

I personally prefer the following bash function:

docker_housekeeping() {

  # Collect list of images older than 4 weeks.
  OLD_IMAGES=$((docker images |
                grep -P "(months|[4-9] weeks|\d\d+ weeks)" |
                awk '{print $3}'))

  # Remove old images.
  docker rmi ${OLD_IMAGES[@]}
}

You can stick it your .bashrc and just call it like docker_housekeeping everytime you want to do clean up.

How To Remove Docker Images, Containers, and Volumes , Use the docker version command on the client to check your client and daemon If -a is specified, will also remove all images not referenced by any container. If there is more than one filter, then pass multiple flags (e.g., --filter "foo=bar" MB golang 1.7.0 138c2e655421 4 months ago 670 MB $ docker image prune -a​  Simple script required to remove all docker images longer than 4 weeks I know similar questions have been asked before, but I cannot find anything that does exactly what I need. I've also asked on the Docker Community forums, but nobody could help. I don't have access to the new docker image prune command, as I cannot u

Try this (Using awk only):

docker images |awk '{if (($4 > 4 && $5 == "weeks") || ($4 > 1 && $5 == "months")) {print $3,$4,$5}}'

Im using if statement, and parenthesis to ensure order of execution.

You don't need to grep.

Remove Untagged Images From Docker, You can remove one or more old or unused Docker images using the To prune volumes, simply add the --volumes flag to the below command as shown. the docker command line tool without the sudo command, you need 4 Jul, 2018 Introducing Learn Linux In One Week and Go from Zero to Hero  How to delete Docker images older than a certain time period? Instructions. This is the process you can follow for deleting the Docker tags older than a certain date: Docker image layers are stored as separate artifacts within an “image” folder. If a layer is already in most Docker clients, it won’t get downloaded often.

You can use the -f flag to add a filter.

Find the image id which is older than 4 weeks old. And execute the following command

docker rmi $(docker images -q -f "before=<docker_image_id_older_than_4_weeks>")

Command to remove all unused images - Feature , Is there a way to remove built docker images some days ago? docker image prune is what I use for all the intermediate images. Are you looking to basically run docker image rm -f on any image 7 days or older? MacOS 10.13, Bash 4.x. I then read that in order to set up Plex in docker, you need to have your HDD's​  –all , -a Show all images (default hides intermediate images) Remove docker image. You can remove unwanted docker images using the following command. docker rmi image_id. In case we need to remove multiple images, we can use filtering command outputs to the input of remove command. See the following example. docker rmi image_id1 image_id2

docker image prune, docker image prune --force --filter "repository=example.com*" by name is just as popular, if not more popular, than removing by time. how to prune old images from one specific registry, but do not delete Windows 10: Docker does not release disk space after deleting all images and containers #244. Bash script helper to remove Docker images and containers.

How to Remove Docker Images, Containers and Volumes, A simple one-line command will tell Docker to get rid of all objects it does not use: For example, if you want to remove all containers older than a day, The above line will remove all containers created before March 5th, 2019, at 4:30 PM. so that you can identify the ID of the image you need to remove. Use the docker volume ls command to locate the volume name or names you wish to delete. Then you can remove one or more volumes with the docker volume rm command: docker volume ls. docker volume rm volume_name volume_name. Remove dangling volumes - Docker 1.9 and later. Since the point of volumes is to exist independent from containers, when a

How to remove docker images which created 7 days ago , Issue the $ docker images command to list the images on the server. HPC visualization containers have differing prerequisites than DGX containers. All of them also include the needed GPU libraries, configuration files, and tools to 4 weeks ago nvcr.io/nvidia/digits 19.03 c4e87f2alebe 5 weeks ago  In order to delete an image that you no longer need, use the docker image rm <image ID> command. Copy the image ID from the IMAGE ID column of the output of docker image ls as shown above. For example, in order to delete the version 7.0.0 image of elasticsearch, use

Comments
  • I would rather use docker inspect $(docker images -q) and find the required field
  • Thanks Farhad but that didn't work - it misses out the image that is 10 weeks old.
  • so much simpler than the other answers.