How to remove docker images based on name?

Docker

Docker Problem Overview


I want to delete all versions of docker images with names that contain a given string (imagename).
I have tried the below, but it doesn't seem to work:

docker images | grep 'imagename' | xargs -I {} docker rmi

Docker Solutions


Solution 1 - Docker

Try the following:

docker rmi $(docker images | grep 'imagename')

or, alternatively:

docker rmi $(docker images 'completeimagename' -a -q)

In Windows PowerShell:

docker rmi $(docker images --format "{{.Repository}}:{{.Tag}}"|findstr "imagename")

Solution 2 - Docker

Slightly more exact version - grepping only on the repository name:

docker rmi $(docker images --format '{{.Repository}}:{{.Tag}}' | grep 'imagename')

Solution 3 - Docker

docker images actually uses the first positional argument as an image name to filter for. No grep's and awk's required. The -q option will return only the matching images IDs which can be fed to docker rmi.

docker rmi --force $(docker images -q 'imagename' | uniq)

The uniq is required to remove duplicates, in case you have the same image tagged differently.

Solution 4 - Docker

docker rmi $(docker images --format '{{.Repository}}:{{.Tag}}' | grep 'imagename')

Solution 5 - Docker

Simply you can add --force at the end of the command. Like:

sudo docker rmi <docker_image_id> --force

To make it more intelligent you can stop any running container before remove the image:

sudo docker stop $(docker ps | grep <your_container_name> | awk '{print $1}')

sudo docker rm $(docker ps | grep <your_container_name> | awk '{print $1}')

sudo docker rmi $(docker images | grep <your_image_name> | awk '{print $3}') --force

In docker ps, $1 is the 1st column i.e. docker container id and $3 is the 3rd column i.e. docker image id

Solution 6 - Docker

I find my answer more simple.

Example, your image name is python_image.

Then your code should be:

docker rmi $(docker images --filter=reference='python_image' --format "{{.ID}}")

I hope this helps.

Solution 7 - Docker

For me worked (Docker version 19.03.5):

docker rmi $(docker images 'imagename' -q)

The command "docker images 'imagename' -q" will list all the images id of the single quoted argument so concatenating it with docker rmi (or docker rmi -f for forcing) it will remove all images with selected name

Solution 8 - Docker

I also got another concise answer. The only change was to remove the unnecessary -I {} flag.

docker images | grep 'imagename' | xargs docker rmi

Solution 9 - Docker

docker rmi `docker images | awk '$1 ~ /imageName/ {print $3}'`

This will remove all the images by name "imageName". In some cases this may give an error like "image is referenced in one or more repositories". In that case use force delete.

docker rmi -f `docker images | awk '$1 ~ /imageName/ {print $3}'`

Another way can be:

docker images | awk '{ OFS = ":" }$1 ~ /imageName/ {print $1,$2}'

Solution 10 - Docker

For some reason I wasn't able to use any of the given answers here. Here's what worked for me.

docker images | grep \"gcr.io/kubernetes-learn-197422/last-week-weather-service\" | awk '{print $3}' | xargs docker rmi

awk '{print $3}' is an important part. It extracts id from the 3rd column.

Solution 11 - Docker

docker rmi $(docker images --filter=reference="IMAGENAME:TAG")

for example if I have the following images

REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
abcdefg/hij                   7.0.0-beta-4.0      3128835950cd        7 days ago          51.4MB
abcdefg/hij-action            7.0.0-beta-4.0      42a7255beb74        7 days ago          97.4MB
abcdefg/hij-test              7.0.0-beta-4.0      17246aed35d0        7 days ago          97.4MB
abcdefg/hij-server            7.0.0-beta-4.0      42c45e561f2c        7 days ago          335MB
abcdefg/hij-init              7.0.0-beta-3.0      f648bb622933        7 days ago          55.2MB
abcdefg/hij-dir               7.0.0-beta-3.0      0db07d3aaccf        7 days ago          97.4MB
abcdefg/hij-mount             7.0.0-beta-3.0      18d1c0e1502c        4 weeks ago         33.7MB

Then docker rmi $(docker images --filter=reference="abcd*:*4.0") will remove the first four images.

Further information please see https://docs.docker.com/engine/reference/commandline/images/

Solution 12 - Docker

docker rmi $(docker image ls repo/image_name* | awk {'print $1":"$2'} )

Solution 13 - Docker

Put together a solution to this question to meet my needs, that wasn't posted previously.

I wanted to match locally built images: library/myapp that I've renamed, tagged, and pushed to a private repo: myrepo.org/library/myapp. Once they are pushed, I want to clean up the local docker reg.

And I don't care if nothing is found or the command throws an error. Thus added || true at the end for inclusion into scripts.

docker rmi $(docker image ls */library/myApp --format '{{.Repository}}:{{.Tag}}') || true

Solution 14 - Docker

Minor remark: From what we are experiencing, it seems you're - since docker 18.03 - no longer able to remove untagged images based on just the name. You either need to use the name+tag as stated above or use the ID.

docker images --format={{.ID}} | xargs -I % sh -c 'docker rmi --force % 2>&1'

Solution 15 - Docker

Based on @Aditya's answer, you can also create a bash function to remove images at ease. Put this function to your ~/.profile. Create a new shell session or source it by source ~/.profile.

function drmi() {
    docker images | grep $1 |  xargs docker rmi
}

Usage:

drmi golang

Solution 16 - Docker

Informational:

docker rmi $(docker images --format "{{.Repository}}:{{.Tag}}" |grep imagename |grep none)

Give this error:

> Error response from daemon: invalid reference format

Probably because my images looks like this:

hostname:5000/imagename:<none>

I had to use the image id and do this command:

docker rmi `docker images --format "{{.Repository}}:{{.Tag}} {{.ID}}" |grep imagename |grep none |cut -d ' ' -f2`

To remove exited containers:

docker container rm `docker container ls -a --format "{{.ID}} {{.Image}} {{.Status}}" |grep Exited |grep imagename|cut -d ' ' -f1`

Solution 17 - Docker

Based on my preferred answer but with a variable instead:

docker rmi --force "$(docker images -q $repository | uniq)"

And this could be used in a Jenkins pipeline:

sh(script: 'docker rmi --force "$(docker images -q $repository | uniq)"', returnStdout: true)

I preferred this option instead of removing through the tag:

$ docker rmi test:latest

This is because, as the docs say and I verified, "using this command with the tag as a parameter only removes the tag. If the tag is the only one for the image, both the image and the tag are removed."

Solution 18 - Docker

Try this out:

docker images | grep 'DOCKER_IMAGE_NAME' | xargs -l bash -c 'docker rmi $0:$1' | xargs

Solution 19 - Docker

To remove image from my local I used following command it worked!!

docker rmi imagename

When I was using image name in single quote it didn't worked.

Solution 20 - Docker

docker image ls | grep 'repository-name' |  awk '{ print $3 }'  | while read -r line ; do docker image rm $line; done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAditya SinghView Question on Stackoverflow
Solution 1 - DockerRamblerView Answer on Stackoverflow
Solution 2 - DockerElton StonemanView Answer on Stackoverflow
Solution 3 - DockerudondanView Answer on Stackoverflow
Solution 4 - DockertheoneandonlyakView Answer on Stackoverflow
Solution 5 - DockerkushalbhattadView Answer on Stackoverflow
Solution 6 - DockerReamon C. SumapigView Answer on Stackoverflow
Solution 7 - DockerGiuse PetrosoView Answer on Stackoverflow
Solution 8 - DockerAditya SinghView Answer on Stackoverflow
Solution 9 - Dockerzaman sakibView Answer on Stackoverflow
Solution 10 - DockersergeyzView Answer on Stackoverflow
Solution 11 - DockerHarryQView Answer on Stackoverflow
Solution 12 - DockerMichael A.View Answer on Stackoverflow
Solution 13 - DockerJohnView Answer on Stackoverflow
Solution 14 - DockerTim ChaubetView Answer on Stackoverflow
Solution 15 - DockerHalil KaskavalciView Answer on Stackoverflow
Solution 16 - DockerMartin P.View Answer on Stackoverflow
Solution 17 - DockerNagevView Answer on Stackoverflow
Solution 18 - DockerAchyut SarmaView Answer on Stackoverflow
Solution 19 - DockerMadanDView Answer on Stackoverflow
Solution 20 - DockerSulaymon HursanovView Answer on Stackoverflow