How To Delete All Docker Images

If you’re in need of a comprehensive, open-source container platform for creating independence between apps and infrastructure, Docker is your safest bet. If you’ve had the chance to use it, you already know how capable and easy to use it is

How To Delete All Docker Images

A large number of cloud and IT companies find it extremely useful, and its popularity is definitely deserved.

Still, this doesn’t make it perfect. The proof can be seen in the limitations that come with removing images, volumes, containers, and networks. Even though these commands are available, they’re not as comprehensive as the users might want them to be.

Here you’ll see the main problems that users encounter, as well as the solutions for them. You’ll learn how to remove images and containers in an easy way.

The Issue

Container technology provides a way of visualizing operating systems. It allows an app to be packaged with all it takes to run it, which allows it to act independently from the OS.

Container images are self-contained executable app packages that include everything that’s needed for an app to work properly. Runtime, code, configurations, and system tools and libraries are contained in each image.

As you use Docker, these images accumulate. Over time, a large number of unused images, data volumes, and containers is accumulated, creating a crowded Docker environment. When this happens, it’s necessary to refresh the environment so that the platform works as it should.

There are multiple commands that can make this happen, so let’s take a look at the main ways of de-cluttering your Docker environment.

Removing Docker Images

Before you can remove the unneeded images, you need to distinguish them from the useful ones. This can be done easily by listing all the images that your system contains by using the image management command.

Here’s what it looks like:

$ docker image #list the most recently created images

Or

$ docker image -a      #list all images

With the second command, you’ll see all your docker images. All you have to do is find the so-called ‘dangling images’. These are all Docker images that don’t contain a tag. Without the tag, there’s no connection with the tagged images, which means that they’re not useful anymore.

You can delete one or more images by using the Image ID. To do this, use one of these commands:

$ docker rmi d65c4d6a3580                            #remove a single image

$ docker rmi 612866ff4869 e19e33310e49 abe0cd4b2ebc  #remove multiple images

A more convenient solution is to use the -f filter flag to list all dangling images.

To delete these images and clean up disk space, use one of the following commands:

$ docker image prune           #interactively remove dangling images

Or

$ docker rmi $(docker images -q -f dangling=true)

You can remove all Docker images in a similar way. List them by using the $ docker images –a command then remove them by using the following:

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

Another option is to delete the images that follow a certain pattern. To list them, use the following command:

$ docker images -a | grep "pattern"

Then, remove them by using:

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

Removing Containers

Another thing you might want to do after working with Docker for a certain amount of time is delete one or more containers.

Similar to image removal, you first need to list them by using the following command:

$ docker ps

Or

$ docker ps -a

When you find the containers you want removed, use their ID to do it.

$ docker rm 0fd99ee0cb61              #remove a single container

$ docker rm 0fd99ee0cb61 0fd99ee0cb61 #remove multiple containers

In case the container you’re trying to remove is running, you can use the following command to stop it:

$ docker stop 0fd99ee0cb61

$ docker rm -f 0fd99ee0cb61

Lastly, you can stop and remove all unnecessary containers by using the following commands:

$ docker stop $(docker ps -a -q)      #stop all containers

$ docker container prune              #interactively remove all stopped containers

Or

$ docker rm $(docker ps -qa)

This will let you free up valuable disk space and start fresh.

The Final Word

As you can see, removing Docker images and containers isn’t as daunting as it may. Now that you know these commands, you can have more control over you Docker experience.

Of course, these are only some of the commands, and you can use a variety of other combinations. Still, these will do the job just fine, so feel free to try them out.

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.