How To Remove All Docker Images Easily On Linux
Docker simplifies application deployment, but managing images is crucial to keeping your environment clean. This quick guide walks you through the steps to remove all Docker images and free up valuable disk space.
Hey there! Looking for an easy solution/command for how to remove all Docker images easily? Then you have come to the right place! 💪
In this short tutorial on Docker, I will show you how easy it is to delete all Docker images and delete all unused Docker images with only a few commands in the terminal.
Remove/Delete All Docker Images
If you would like to simply just remove/delete all your locally installed Docker images from your system, it's easily done by combining
docker rmi and the
docker images command, as shown below.
docker rmi $(docker images -a -q)
What happens in the command above? 🤔
- We use the
docker imagescommand to show the list of all local images in the system. When used in combination with the
-aflag (short for
--all), we get all the images. The
-qflag (short for
--quiet) is used to only return the Docker image ID.
- Finally, we have the
docker rmi, command, which will delete one or more specific images. These images will come from the previously described
To illustrate what happens behind the scenes, you can take a look at this example, as that is what's happening in your terminal when you run the above command.
docker rmi f7fdab215ab7 16fc8e1e7421 9a7dbf989d60
The output above shows the deletion of three images.
Remove Only Unused Docker Images
If you are doing housekeeping and would like to free up some space by removing/deleting all unused Docker images, then use the following command in your terminal.
docker image prune
If you would like to remove all unused images including dangling images, you can use the
-a flag (short for
--all), just like I have done below.
docker image prune -a
Remove All Images Including Containers
If you would like to remove everything and have an empty Docker environment, then you will be happy with the following command.
Stopping All Running Containers
To delete a Docker image, we have to stop any running container that uses that specific image. To do this we can combine the commands again, just like I have done below.
docker stop $(docker ps -q)
docker stop will stop a container by a specific ID but
docker ps -q will list each ID of all running containers in our environment.
Each container ID should be listed in your terminal, once they are stopped, like shown below.
schou@Christians-MBP ~> docker stop $(docker ps -q)
Removing All Containers
Once you have stopped all running containers, it's time to remove them so the images will get detached from them. You can do this by using the command below.
docker rm $(docker ps -a -q)
As before, we use the
-q flag again to only list the ID of the containers and then the
-aones flag to get all containers including the stopped onces. The command
docker pswill list containers.
Great, now we can remove the images using the command from earlier.
docker rmi $(docker images -a -q)
One Command To Rule Them All
There is no such thing... 😅 However if you would like to do all these three tasks in one go, you can run the following command.
docker stop $(docker ps -q) && docker rm -f $(docker ps -aq) && docker rmi -f $(docker images -q)
Here are some common questions I often encounter when telling people how to do the above tasks.
What Is The Difference Between docker rmi and docker image prune?
docker rmi and
docker image prune are commands in Docker that are used to remove Docker images, but they have different purposes and functionality.
docker rmicommand is used to remove one or more Docker images from the local machine.
docker image prunecommand is used to remove all dangling images and, optionally, unused images to free up disk space.
What Is The Difference Between An Image And A Container?
An image is a lightweight, standalone, and executable package that includes everything needed to run a piece of software. A container is an instance of an image that runs as a process on a host system.
I hope you solved the issue you had. If not, please let me know in the comments below.
In this short Docker tutorial, you learned how to do some Docker image housekeeping to free up space from unused images. You learned how to stop all running containers and clean up after them.
If you have any questions or suggestions, please let me know in the comments below. ✌️