Mass-Deleting Docker Images

I’m having a cleanup of my Docker images and there’s a bit of a mismatch between the output format of docker images and the input of docker rmi. I don’t however want to delete everything, only a selection of images.

Luckily there’s a –format argument to docker images which allows an output format to be specified.  Here’s the trick:

$ docker images --format "{{.Repository}}:{{.Tag}}" | \
    grep :foo \
    xargs docker rmi

This command deletes all images with the tag “foo”, something which is tricky using the standard output format.

The documentation for the docker images command has all the details.