Signup/Sign In

How to remove/delete Docker images from system?

When you work with Docker, a lot of times, you have to create multiple images to test different changes. You tag each image and use the latest one, and generally forget about the older docker images. But over a period of time, as more docker images get collected in your system, they will clutter your file system and will also consume disk space.

So you must delete or remove docker images from your system if you are not using them.

But how can you remove docker images that are not getting used, or dangling docker images?

Dangling docker images refers to the images that are not tagged or that are not associated with any container.

In order to remove or delete any docker image, you must first list down all the docker images, find the one that you want to remove, and then use the docker command to remove/delete the docker image from your system.

List all Docker images

You can start by listing down all the docker images available in your system. To list down all the docker images that are stored in your system, you can use the docker images command along with the -a flag.

docker images -a

list all docker images

This command will list down all the docker images, along with TAG, IMAGE ID, etc. that you can use to remove the docker image from your local system.

Remove a specific Docker Image

To remove one or more docker images, you can use the Image ID to remove the docker image.

You can use the following command to remove a specific docker image:

docker rmi [IMAGE-ID or REPOSITORY]

In the above command, you have to provide the Image Id for the docker image that you want to remove, or you can provide the name of the repository that you get from the docker images command.

For example,

docker rmi docker/welcome-to-docker

or, you can use the Image Id,

docker rmi b1eb53af308a

Remove Dangling Docker images

Any image that you create without tagging it, the image is treated as a dangling image by docker.

You can list down all the dangling images by using the flag -f along with the value of dangling=true to the docker images command.

docker images -f dangling=true

Once you have seen all the dangling docker images, you can use the prune command:

docker image prune

Remove images using pattern

You can also use a regular expression to match and find multiple docker images.

For using a pattern to match docker images and then remove those images, you will have to use multiple docker and linux commands together.

The commands that you will have to run are docker images, grep command to search docker images using a pattern, then use the awk command to supply the docker image IDs to the docker rmi command to remove the docker images.

To list all the docker images that matches a pattern, you have to run the following command:

docker images -a |  grep "some-pattern"

And to remove the listed docker images, run the following command,

docker images -a | grep "some-pattern" | awk '{print $3}' | xargs docker rmi

Remove all Docker images

If you want to remove all docker images, you can do it by running a single command.

You can pass the result of the docker images command to the docker rmi command.

docker rmi $(docker images -a -q)

And that will remove or delete all the docker images from your system.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight