How can I delete Docker images by tag, preferably with wildcarding?

Fun with bash:

docker rmi $(docker images | grep stuff_ | tr -s ' ' | cut -d ' ' -f 3)

only using docker

docker rmi $(docker images -q junk/root:*)

You can also acomplish it using grep + args + xargs:

docker images | grep "stuff_" | awk '{print $1 ":" $2}' | xargs docker rmi
  • docker images lists all the images
  • grep selects the lines based on the search for "_stuff"
  • awk will print the first and second arguments of those lines (the image name and tag name) with a colon in between
  • xargs will run the command 'docker rmi' using every line returned by awk as it's argument

Note: be careful with the grep search, as it could also match on something besides the tag, so best do a dry run first:

docker images | grep "stuff_" | awk '{print $1 ":" $2}' | xargs -n1 echo
  • xargs -n1 the -n1 flags means xargs will not group the lines returned by awk together, but echo them out one at a time (for better readability)

Using only docker filtering:

 docker rmi $(docker images --filter=reference="*:stuff_*" -q)
  • reference="*:stuff_*" filter allows you to filter images using a wildcard;
  • -q option is for displaying only image IDs.

Update: Wildcards are matched like paths. That means if your image id is my-company/my-project/my-service:v123 then the * won't match it, but the */*/* will. See the github issue for description.

Tags:

Docker