DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

How to Remove Docker Images Based on Names

We are going to use Docker's rmi command.

Docker rmi command removes (and un-tags) one or more images from the host. If an image has multiple tags, 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.

$ docker rmi $(docker images <your_docker_image_name>)
Enter fullscreen mode Exit fullscreen mode

Example:

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             1.32.0              dc3bacd8b5ea        16 hours ago        1.23MB

$ docker rmi busybox
untagged: busybox:1.32.0
Enter fullscreen mode Exit fullscreen mode

Multiple Tags

If you have different tags for same image name;

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             1.32.0              dc3bacd8b5ea        16 hours ago        1.23MB
busybox             latest              dc3bacd8b5ea        16 hours ago        1.23MB
busybox             1.31                1c35c4412082        5 months ago        1.22MB
Enter fullscreen mode Exit fullscreen mode

You should use it with -q flag:

$ docker rmi $(docker images -q <your_docker_image_name>)
Enter fullscreen mode Exit fullscreen mode

The -q flag returns only the matching images IDs which can be fed to docker rmi.

Error

If you encounter an error like below you have to use it with -f (force) flag:

Error response from daemon: conflict: unable to delete dc3bacd8b5ea (must be forced) - image is referenced in multiple repositories
Enter fullscreen mode Exit fullscreen mode
$ docker rmi -f $(docker images -q <your_docker_image_name>)
Enter fullscreen mode Exit fullscreen mode

All done!

Top comments (0)