Remove All Docker Images

Last Updated on 2023-05-24

The short answer

To remove (delete) all locally installed Docker images from your machine, you can use a combination of the  [.inline-code]docker rmi[.inline-code]  and the  [.inline-code]docker images [.inline-code]  commands as follows:

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

Here, the  [.inline-code]docker images [.inline-code]  command is used to show the list of all locally installed images. The  [.inline-code]-a [.inline-code]  flag (short for  [.inline-code]--all [.inline-code])  is used to list all images, including the intermediate images that are hidden by default. The  [.inline-code]-q [.inline-code]  flag (short for  [.inline-code]--quiet[.inline-code])  is used to only show the image ID.

The  [.inline-code]docker rmi[.inline-code]  command, on the other hand, is used to delete one or more specific images.

Here is an example of what this command may look like right before being executed by the shell:

 $ docker rmi 0c717bfd9ec0 3e4394f6b72f a8780b506fa4

Note that if one or more containers are still using any of the images you are trying to delete, Docker will output an error message saying that the image is in use and cannot be deleted. In this case, you will need to stop and remove those containers first before you can delete all images (see section below).

[#easily-recall-with-ai]Easily retrieve this command using Warp’s AI Command Search[#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering  [.inline-code]docker remove all images [.inline-code]  in the AI Command Search will prompt a  [.inline-code]docker [.inline-code]  command that can then quickly be inserted into your shell by doing  [.inline-code]CMD+ENTER [.inline-code].

[#remove-unused-images-only]Remove unused images only[#remove-unused-images-only]

To delete all dangling images, which means images that have neither a repository nor a name tag, you can use the following  [.inline-code]docker [.inline-code]  command:

 $ docker image prune

To delete all unused images (i.e. not referenced by any container) including dangling ones, you can use the  [.inline-code]-a [.inline-code]  flag (short for  [.inline-code]--all [.inline-code]) as follows:

 $ docker image prune -a

[#remove-all-images]Remove all images and containers at once[#remove-all-images]

To remove all images and containers on your local machine, you must first stop any currently running container using a combination of the  [.inline-code]docker stop [.inline-code]  command and the  [.inline-code]docker ps [.inline-code]  command:

 $ docker stop $(docker ps -q)

Where  [.inline-code]docker stop [.inline-code]  will stop running containers and  [.inline-code]docker ps -q [.inline-code]  will list the ID of all running containers. 

Once all containers are stopped, you can remove them using the  [.inline-code]docker rm [.inline-code]  command:

 $ docker rm $(docker ps -a -q)

Where the  [.inline-code]-a [.inline-code]  flag of the  [.inline-code]docker ps [.inline-code]  command is used to list all containers including stopped ones. See our post on removing stopped Docker containers for more details.

Finally, you can remove all locally installed images using the  [.inline-code]docker rmi [.inline-code]  command:

 $ docker rmi $(docker images -aq)

[#docker-rmi]The difference between [.inline-code]docker rmi [.inline-code]  and [.inline-code]docker image prune [.inline-code]  [#docker-rmi]

While both commands are used to deal with the removal of Docker images, they have different functionalities.

The  [.inline-code]docker rmi [.inline-code]  command is used to delete one or more specific Docker images based on their name or identifier.

The  [.inline-code]docker image prune [.inline-code]  command, on the other hand, is used to clean up all unused Docker images including dangling ones.