How to remove Docker Images?


We can use the Docker image rm, Docker rmi, and Docker image prune commands to remove or un-tag one or multiple Docker images from the host machine. Both the commands can be used to delete or remove Docker images. We can use this command with the tag as a parameter to remove images that have multiple tags. We can also combine the Docker rmi command with a sub-command to remove all Docker images at once.

However, the Docker image rm or rmi commands do not delete or remove images from the registry. Moreover, if an image has active containers associated with it, you cannot remove the image unless you use the -f (force) option.

In this article, we will discuss how to remove docker images from a host machine through various commands. Before we move ahead, you should check out the following guides to udnerstand the topic in a better way.

  1. What is Docker?
  2. What are Docker Images?
  3. How to Build Docker Images?

Remove Docker Images

We can remove Docker images using three different ways. Let’s check out all of them one by one.

Docker Image rm Command

We can use this command to delete Docker images (one or more) from the host machine. The general syntax of the Docker image rm command is –

$ docker image rm [OPTIONS] IMAGE [IMAGE...]

The options that can be used along with this command is –

NameShorthandDescription
--force-fTo remove docker images forcefully.
--no-pruneTo avoid deleting untagged parent images.
Options for Docker Image rm command

Let’s list all the Docker images first.

List Docker Images
List Docker Images

Let’s try to remove the fedora image with tag 23 using the Docker image rm command.

$ docker image rm fedora:23
Docker Image Remove
Docker Image Remove

We can list the images once again to verify the removal of the image.

Image list
Image List

We can see that the fedora:23 image has been removed.

Now, for the hello-world image, we have a container associated with it. To check this, we can list all the Docker containers.

$ docker container ls -a
Docker Containers
Docker Containers

If we try to remove this image, it will throw an error. Let’s check this out.

$ docker image rm hello-world
Removing image with active container
Removing image with active container

To avoid this, we can either stop the container and then remove this image, or use the force option along with the Docker image rm command. Let’s use the second one.

$ docker image rm -f hello-world

You can see that the image has now been deleted.

Force Remove an image
Force Remove an image

Delete Image Using Docker rmi Command

We can also use the Docker rmi command to delete Docker images. The syntax of this command is –

$ docker rmi [OPTIONS] IMAGE [IMAGE...]

Let’s try to delete the busybox image.

$ docker rmi busybox
Docker rmi command
Docker rmi command

We can use the -f (force) option here as well. Let’s try to forcefully delete the flaskapp image for which we have a container created.

$ docker container ls -a
List docker containers
List containers

Let’s try to remove the flaskapp image.

Delete Docker Images
Delete Docker Images

You can see that the image has been removed.

Delete Docker Images using Docker Image Prune Command

The Docker image prune command is used to remove all the dangling images. Dangling images are those that are just image layers and have no use now. The syntax of the Docker prune command is –

$ docker image prune [OPTIONS]

The options that we can use are –

NameShorthandDescription
--all -aTo remove or delete all unused images and not just the dangling ones.
--filterYou can provide filter values such as ‘until=<timestamp>’.
--force -fYou can remove images forcefully without any prompt for confirmation.
Options for Docker Image Prune Command

Let’s first list all the Docker images.

Docker Image List
Docker Image List

Now, let’s prune the dangling and unused images using the –all option.

$ docker image prune --all
Delete Docker Images using Docker prune command
Delete Docker Images using Docker prune command

Now, let’s list all the images once again.

List Docker Images
List Docker Images

You can see that all the Docker images have been removed.

How to delete all Docker Images at once?

To delete all the Docker images at once, you can use the following command.

$ docker rmi -f $(docker images -a -q)

In the parent command, we have used the docker rmi command with the force removal option. In place of the image name, we have mentioned a sub-command which returns the list of image IDs of all the images quietly.

Let’s list all the Docker images first.

List all Docker images
List all Docker Images

Now, let’s remove all of them using the above-mentioned command.

Remove all Docker images
Remove all Docker images

If we list all the Docker images again, we will find that there is no image available now.

Docker Image List

Final Thoughts!

To sum up, in this article we discussed how to delete or remove Docker images from the host. We discussed 3 different method to do so – Docker image rm, Docker rmi, and Docker image prune commands. Finally, we also discussed how to remove all Docker images at once.

If you have any queries or suggestions, please mention them in the comments and we will have our experts get back to you. Also, check out complete and free Docker Tutorials.

Recommened Articles –

  1. What is Docker?
  2. What are Docker Images?
  3. How to Pull Docker Images?
  4. How to List Docker Images?

Leave a Reply