I recently had to delete a number of docker images from my local machine. The command for removing images has the following format:

docker image rm [OPTIONS] <image_id_1> [<image_id_2> <image_id_3 ...]

My initial instinct was to use a file glob, to see if that worked.

docker image rm foo*

However it turns out that you can’t do this. The way to achieve this instead is to use the result of a listing call to generate the required IDs. For example, let’s say you want to remove all images from your machine that started with the word foo. You can list the images that have a REPOSITORY name that includes the term “foo” with the command:

$ docker images foo*

foo_web       latest    875f314768f4    5 months ago    123MB
foo_worker    latest    12ab875f3147    3 months ago    456MB

The output of the docker images command is a table by default, but we only want the image IDs so that we can pass them to the docker image rm command. We can do this with the -q option:

$ docker images -q foo*


Putting the commands together we get:

$ docker image rm $(docker images -q foo*)

or if you use Fish shell:

# don't include $ before parentheses and quote the glob
$ docker image rm (docker images -q 'foo*')