How do I delete multiple docker images in a single command?

Hi, I would like to remove a number of mailcow images. It has different image names but owned by a same user which is mailcow.

How may I remove those mailcow images in a single command? Can I use RegEx ?

Thank you.

Hello,

based on this StackOverflow-article I have created the following one-liner

docker rmi `docker image ls | egrep "^mailcow/" | awk '{print$1}'`

At least it was working on my linux-box :slight_smile:

Thanks Matthias, I tried it but somehow the images still persisted :

First, I tried the template string command to ensure the images are shown in the result. It did.
Second, I executed the whole command but it did not discover mailcow images (It’s wierd).
Last, I listed docker images and mailcow images were still there.

I have no idea what went wrong…

Uups, sorry - only tested with latest as tag.
So instead you can use the image ID to delete the wanted images

docker rmi `docker image ls | egrep "^mailcow/" | awk '{print $3}'`

or concat the repository and the tag

docker rmi `docker image ls | egrep "^mailcow/" | awk '{print $1":"$2}'`
1 Like

Big thanks @matthiasradde !

Finally I think I got it. So, docker image ls displays repository, tag, and image id, etc. in order, just like the result of docker image ls in the picture I sent above (I can’t send more than one media in a reply due to terms of DockerHub, sorry).

Therefore this one displays repository :

docker image ls | egrep "^mailcow/" | awk '{print $1}'

Meanwhile this one gets tag :

docker image ls | egrep "^mailcow/" | awk '{print $2}'

Then this command fetches image id :

docker image ls | egrep "^mailcow/" | awk '{print $3}'

The result of each command :

==================================

Removing a docker image can be done through either :

docker rmi <repository>:<tag>

or

docker rmi <image id>

(note)
<repository>:<tag> = <username/image name>:<tag> such as mailcow/unbound:1.14 (username=mailcow, image name=unbound, and tag=1.14).

Hence, the command below deletes docker images by fetching the image id :

docker rmi `docker image ls | egrep "^mailcow/" | awk '{print$3}'`

Please correct me if there’s something wrong…