Unused Docker images can pile up over time and occupy considerable disk space. Removing these images efficiently helps recover storage and ensures your system remains clutter-free.
This guide will walk you through the process of removing unnecessary Docker images from your system. But before diving in, let’s briefly touch upon Docker, its images, and where these images are typically stored.
What is Docker?
Docker has become a robust and trustworthy tool for creating, testing, and launching applications since it was introduced more than ten years ago. It is a platform that is open-source and employs OS-level virtualization to enable the packaging of applications and their environments within containers.
What are Docker Images?
Docker containers are created using immutable templates known as Docker images. A Docker image holds all the necessary components for an application to operate, such as its code, dependencies, and configuration.
Containers made from these images are independent units that are lightweight, self-contained, and isolated, running separately from the host operating system. Containers, in contrast to virtual machines, are easier to move, effective, and require fewer resources as they do not rely on a standalone operating system. Containers ensure that the application performs consistently in various environments, regardless of deployment location.
Nevertheless, Docker containers and images have the potential to amass rapidly on your computer, taking up storage space. Fortunately, Docker provides several command-line tools to help manage and remove unused images and containers.
Where are Docker Images stored?
Docker images are often stored in remote repositories or registries, such as Docker Hub, the default registry, or third-party platforms like GitLab. Images can also be stored locally on your system's disk or in cloud-based repositories.
Prerequisites
For this tutorial, you will need a Linux environment with Docker installed and running. We’ll use Ubuntu 20.04 for our examples. If Docker is not yet installed on your system, you may want to follow a guide on how to install Docker on Ubuntu 20.04 before proceeding.
What is docker rm command?
"docker rm" is used to remove one or more Docker containers that are running on the Docker host. A Docker container uses up system resources like CPU, memory, and disk space when it is running. To free up such resources, you may choose to uninstall a container after using it for a while.
sudo docker rm [OPTIONS] CONTAINER [CONTAINER...]
- Options: Various options can be used with the docker rm command to modify its behaviour.
- Container: The name or ID of the Docker container(s) to be removed.
Step-By-Step Process To Remove Docker Container
Step 1: Provide a list of every container on the Docker host.
docker ps -a
Step 2: Stop the docker container before deleting it.
docker stop <Container name> or <Contaier ID>
Step 3: Remove the Docker container when it has stopped; you cannot remove the Docker container unless it has stopped.
docker rm <Container name> or <Container ID>
.webp)
How to Remove Docker Containers?
In this section you will get an idea about multiple ways to remove docker containers
Remove Multiple Docker Containers
You may easily remove a multi-container in Docker by using the following command.
docker rm container1 container2 container3
- docker rm: This is the Docker command for removing containers.
- container1 container2 container3: These are the names or IDs of the Docker containers
.webp)
Force Remove Docker Container
Even when a container is in operation, it can be forcefully removed by using the -f or --force option with the docker rm command. If you wish to forcefully remove a running container, you must use the -f option since Docker does not enable this by default.
docker rm -f CONTAINER_ID_or_NAME
You can see that I attempted to remove the Docker container both with and without the -f option in the following image.
.webp)
Remove All Stopped And Running Containers At Once
Remove All Stopped Containers
To remove all stopped containers in once, use this command.
docker rm $(docker ps -aq)
- docker ps -aq: Lists all Docker container IDs in quiet mode; that is, it prints only the container IDs and nothing else. Each part explained:
- docker ps: List all Docker containers.
- -a or --all: Show all containers, including stopped ones.
- -q or --quiet: Output only container IDs.
- $(...):This is command substitution in Bash. The output of the command inside the parentheses is substituted and executed in the outer command.
- docker rm: This command removes Docker containers.
Remove All running Containers
Add the -f argument to the command above.
docker rm -f $(docker ps -aq)
-(1).webp)
Remove All Exited Containers
To find and remove containers that have exited, first list the exited containers using a filter. After confirming which ones to delete, use the -q
flag to get only the container IDs, which can then be passed to the removal command.
List Exited Containers
To list exited docker containers we can use the following command:
sudo docker ps -a -f status=exited

Remove Exited Containers
To remove exited docker containers we can use the following command:
sudo docker rm $(docker ps -a -f status=exited -q)

Using Multiple Filters to Remove Containers
You can combine multiple filters to select containers that meet different conditions. For instance, if you want to delete containers that are either in a created state or have exited, use the following commands:
List Created or Exited Containers
To show list created or exited containers you can use the following command:
sudo docker ps -a -f status=exited -f status=created

Remove Created or Exited Containers
To remove created or exited containers we can use the following commands:
sudo docker rm $(docker ps -a -f status=exited -f status=created -q)

Remove Containers Based on Name Patterns
To delete containers that match a specific naming pattern, you can use grep
along with docker ps
. After confirming the list, use awk
and xargs
to provide the IDs for deletion. Note that grep
, awk
, and xargs
are not included with Docker and may need to be installed separately:
List Container by Pattern
To list containers that match a specific pattern, use the docker ps -a
command combined with grep
. This allows you to filter the list of all containers by a keyword or pattern.
sudo docker ps -a | grep "<pattern>"
Example
sudo docker ps -a | grep nginx:latest

Remove Containers by Pattern
To remove docker containers by matching pattern we can use the following command:
sudo docker ps -a | grep "nginx:latest" | awk '{print $1}' | xargs docker rm

Remove a Link Between Containers Using --link
Docker has the provision of connecting containers using the --link option, making it possible for direct communication between them. There might be certain situations, however, in which you will need to disconnect these two containers. For example, you want to make sure that the webapp-ubuntu container does not in any way talk to redis-ubuntu; in this case, you would unlink them.
You can do this by using the docker rm --link command. With this, you delete the previously set link, and in effect, kill the means of communication between these 2 containers. Something that comes in very handy when you have to work with managing relationships between containers on Docker’s default bridge network because then you get to have more control over how networks interact.
Unlink /webapp-ubuntu
from /redis-ubuntu
To remove the connection and prevent webapp-ubuntu
from communicating with redis-ubuntu
, use the following command:
sudo docker rm --link /webapp-ubuntu/redis-ubuntu
This command removes the link between the webapp-ubuntu
and redis-ubuntu
containers, cutting off the communication channel that was established using the --link
option.

How to Remove Docker Images?
To find the ID of Docker images on your system, you can use the following command, which lists all images along with their corresponding IDs:
sudo docker images
Remove Single Docker Image
Whenever you are trying to remove a Docker Image it's very easy but it is essential to make sure that you are removing the correct image.
Using the Docker RMI Command
If you want to remove a single Docker image, you can use docker rmi command, where "rmi" stands for "remove image".
To remove a specific image you can use that docker image ID. The below syntax will help you in understanding this command in a better way:
sudo docker rmi image_id
Example:
sudo docker rmi 11329d9fa885
Using the Docker Image RM Command
Both commands docker image rm and docker rmi commands works in a similar way & can be used in an interchangeable way to remove Docker images.
For example, to remove an image using this command, you would execute:
sudo docker image rm
Remove Multiple Docker Images
You can remove multiple images at once by providing multiple image IDs with the docker rmi
command:
For example, to delete images with the IDs a2a15febcdf3
and 4bb46517cac3
, you would run the following command:
sudo docker rmi d2c94e258dcb 35a88802559d
Remove All Docker Images
To remove all images from your Docker system, you can use the following command:
docker rmi $(docker images -q)
Remove Docker Images Based On a Pattern
You can filter and list images matching a specific pattern or tag using docker images
commands combined with grep command
.
Using Patterns to Identify Specific Images
To filter and list Docker images that matches a particular pattern, you can use the docker images
command combined with grep command
. For example, if you want to find all images associated with "my_nginx_image", you would run:
sudo docker images | grep 'my_nginx_image'
Remove Images Based on a Filtered Pattern
To remove a Docker image with "my_nginx_image" in its name, combine the commands as follows:
sudo docker rmi $(docker images | grep 'my_nginx_image' | awk '{print $3}')
Remove Dangling Images
To save disk space and use your Docker environment in better way, it is very important to clean unused dangling images or disk space. Before moving forward it is important to know about dangling images. Dangling images are Docker images that are not tagged or associated with an active or running container.Follow the steps below to create and remove dangling images:
Pull a Base Image
Pull a base image from a Docker registry. This image will be used to create another image that may become a dangling image.
sudo docker pull ubuntu
Create a Dockerfile
Create a Dockerfile that will use the base image. You can add some instructions to modify the image.
Build the Image
Build the image using the Dockerfile. Tag this image to ensure it's not dangling at this point.
sudo docker build -t my-image:latest .
Modify the Image
Make changes to the image, such as adding files or running additional commands. This will create new layers.
Update the Dockerfile
Rebuild the image without changing the tag
sudo docker build -t my-image:latest .
Create a New Tag for the Image
Create a new tag for the image, which will point to the same image layer.
sudo docker tag my-image:latest my-image:temporary
Remove the Tagged Image
Remove the tagged image, which will leave the layers as dangling images.
sudo docker rmi my-image:latest
List Dangling Images
To list dangling images use the following command:
sudo docker images -f "dangling=true"
- Dangling images are generally cleaned up automatically by Docker during a
docker system prune
operation, but you can also manually remove them if needed. - To remove dangling images, you can run:
sudo docker image prune
How to Remove Docker Volumes?
Deleting Specific Volumes
To delete one or more specific Docker volumes, you first need to identify them using the docker volume ls
command. Once you've located the target volume names, you can remove them with the docker volume rm
command:
List Volumes
To list docker volumes we can use the following command:
sudo docker volume ls
Remove Volumes
To remove multiple docker volumes we can use the following command:
sudo docker volume rm volume_name1 volume_name2
Example
sudo docker volume rm volume1 volume2
Remove Unused (Dangling) Volumes
Docker volumes are designed to persist beyond the lifecycle of the container they are attached to. When a container is deleted, the associated volumes are not automatically deleted. Volumes that are no longer attached to a container are called hanging volumes.
You can list these dangling volumes using a filter with docker volume ls. Once identified, you can remove them using the docker volume prune command.
List Dangling Volumes
To list dangling volumes we can the following command:
sudo docker volume ls -f dangling=true
docker volume ls
: This lists all the volumes on the Docker system.-f dangling=true
: This filter (-f
) ensures that only dangling (unused) volumes are displayed. "Dangling" refers to volumes that were once used by a container but are no longer in use, making them candidates for cleanup.
This command helps in identifying volumes that can be safely removed to free up disk space
Remove Dangling Volumes
To remove dangling volumes we can use the following command:
sudo docker volume prune
Remove a Container Along with its Volume
If you want to remove a container and its associated unnamed volume, you can use the -v
flag with the docker rm
command. This flag only works for unnamed volumes—when the container is deleted, the unnamed volume will also be removed automatically. Named volumes, however, will persist after container removal.
Remove Container and Unnamed Volume
sudo docker rm -v container_name
Example
sudo docker rm -v my_nginx_container
Conclusion
To sum up, the docker rm command is an effective tool for eliminating Docker containers from your computer. It enables you to remove containers that are no longer required, freeing up system resources. The name or ID of the container(s) you wish to delete should be specified in the command's basic format, docker rm [OPTIONS] CONTAINER [CONTAINER...]. The -f or --force option allows you to force the removal of running containers, however by default the operation simply removes halted containers. Furthermore, you can eliminate multiple containers, filter containers according to particular standards, or eliminate containers in bulk by combining different commands and parameters. Use caution at all times while executing the docker rm command, especially when the -f option is present, to prevent unexpected.
People Also Read
Docker |
---|
Docker Commands | Read |
---|
Dockerfile | Read |
---|
Docker Tutorials | Read |
---|
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What Is Cloud Computing ? Types, Architecture, Examples and Benefits Nowadays, Cloud computing is adopted by every company, whether it is an MNC or a startup many are still migrating towards it because of the cost-cutting, lesser maintenance, and the increased capacity of the data with the help of servers maintained by the cloud providers. Cloud Computing means stori
15 min read