How to delete Docker images step by step

Docker images can take up considerable disk space on your host system and in your container registry. When you delete an unused or outdated Docker image, you free up space for new images and data. This is particularly useful in environments where disk space is limited.

When should you delete a Docker image?

Docker images are created from Dockerfiles, which outline how to configure container infrastructure. These images are used to initiate Docker containers, which run applications or services in an isolated and consistent environment.

Having a high number of images can affect the performance of your Docker infrastructure and take up an excessive amount of disk space. When Docker searches for an image, it has to go through all available images to find the right one. That’s why deleting an unneeded Docker image can increase efficiency and reduce search times. Deleting Docker images you don’t need also protects your system from security risks.

When development and test cycles are completed, you should delete any images that were needed for specific tasks during the cycles but are now no longer needed. Doing so will help you to free up disk space.

How to delete one or more Docker images

If you want to delete one Docker image or multiple Docker images at the same time, you can pass a list of the image IDs or names to the docker rmi command.

Step 1: List image IDs and tags

Use the following command to display a list of all Docker images on your system:

docker images -a
bash

The output contains information such as the image name, the image ID, the creation date, the size and the creator of the image.

Step 2: Delete images

If you want to remove several images at the same time, you can write the image IDs or image names one after the other and separate them with spaces.

docker rmi Image Image
bash

Keep in mind that you can only delete a Docker image if no active containers are using it. Before deleting images, you’ll need to first stop and remove the containers that are using them.

How to delete unused Docker images

Unused Docker images are images that are no longer being used by other images or active containers. These unreferenced or unused images can accumulate over time and take up disk space on your Docker host. It’s best practice to delete these images on a regular basis so your Docker environment can run as efficiently as possible.

Step 1: List unused images

To check which images you should delete, you can generate a list of unused images.

docker images -f dangling=true
bash

Step 2: Delete unused images

The following command deletes all unused images, freeing up the storage space that the images once occupied.

docker images purge
bash

How to delete Docker images based on a pattern

Deleting Docker images based on a specific pattern or prefix allows you to remove a group of images that share something in common. This is useful if you have a large number of images and only want to delete ones that fall into a specific category.

Step 1: List Docker images using grep

You can combine the docker images command with grep so that only the images that match the pattern are displayed.

docker images -a | grep "pattern"
bash

Step 2: Delete Docker images

Once you have decided on a pattern, you can filter images that match the pattern with awk and extract the image IDs. You can then delete the images using xargs docker rmi.

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

How to delete all Docker images

You should be very careful when deleting all Docker images from your system as this step is irreversible and may result in your containers not starting.

Step 1: List all images

The first step is to get an overview of all active and inactive images on your system:

docker images -a
bash

Step 2: Delete all Docker images

You can now pass the output of the previous step to the docker rmi command. This will delete all your Docker images.

docker rmi $(Docker-Images -a -q)
bash

Instructions on deleting Docker volumes and removing Docker containers can be found in our Digital Guide along with general information about Docker and a detailed explanation of Docker container volumes.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top