How to Stop & remove all or running or stopped docker containers ?

In this article we will discuss how remove all docker containers or only running or stopped containers.

Suppose we have 6 containers in our docker engine out of which 3 are running and 3 are stopped i.e.

PS C:\Windows\system32> docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                   PORTS               NAMES
4188498ee191        ubuntu              "/bin/bash"         7 seconds ago       Up 6 seconds                                 confident_napier
fe3499578648        centos              "/bin/bash"         18 seconds ago      Up 17 seconds                                condescending_vaughan
4622f48ceaed        ubuntu              "/bin/bash"         4 hours ago         Exited (0) 4 hours ago                       hungry_hofstadter
ecc42527bba4        ubuntu              "/bin/bash"         4 hours ago         Exited (0) 4 hours ago                       sharp_kapitsa
6f5190548f7a        centos              "/bin/bash"         4 hours ago         Exited (0) 4 hours ago                       loving_cocks
6708084317a1        centos              "/bin/bash"         4 hours ago         Exited (0) 4 hours ago                       jovial_diffie
af70224e7cc5        centos              "/bin/bash"         4 hours ago         Exited (0) 4 hours ago                       objective_haibt
121ba587209b        centos              "/bin/bash"         4 hours ago         Exited (0) 4 hours ago                       flamboyant_nobel

Remove all Running Docker Containers

As we saw above that we have two running containers, now to delete all the running containers use following command,

docker rm -f $(docker ps -q)

It will forcefully delete all the running containers (no need to stop them first). but how did it worked ?

How did it worked ?

docker rm command needs IDs or names of the containers to be deleted. But if we want 100 containers to be deleted then it is really hard to type all the container IDs in a single command. Therefore, to make our life easy first we fetched the IDs of all running containers using following command,

docker ps -q

It returned the Ids of all running containers i.e.

4188498ee191
fe3499578648

Then we passed these list of container IDs to docker rm command i.e.

docker rm -f $(docker ps -q)

This is how it removed all running containers. Also, as we are directly removing the containers without first stopping them. Therefore we used the -f option to forcefully remove the running containers.

How to Remove all stopped Docker Containers ?

Many times we have large number of stopped containers in our system and we want to delete all of the stopped containers. Let’s see how to do that

Suppose we have total 8 containers out of which 6 are stopped containers i.e. containers with state ‘Exited’ i.e.

PS C:\Windows\system32> docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                   PORTS               NAMES
4e88ee8e736e        centos              "/bin/bash"         About a minute ago   Up About a minute                            agitated_chaplygin
8b38ae16205a        ubuntu              "/bin/bash"         About a minute ago   Up About a minute                            silly_napier
4622f48ceaed        ubuntu              "/bin/bash"         5 hours ago          Exited (0) 5 hours ago                       hungry_hofstadter
ecc42527bba4        ubuntu              "/bin/bash"         5 hours ago          Exited (0) 5 hours ago                       sharp_kapitsa
6f5190548f7a        centos              "/bin/bash"         5 hours ago          Exited (0) 5 hours ago                       loving_cocks
6708084317a1        centos              "/bin/bash"         5 hours ago          Exited (0) 5 hours ago                       jovial_diffie
af70224e7cc5        centos              "/bin/bash"         5 hours ago          Exited (0) 5 hours ago                       objective_haibt
121ba587209b        centos              "/bin/bash"         5 hours ago          Exited (0) 5 hours ago                       flamboyant_nobel

Now to delete all the stopped containers use following command,

docker rm $(docker ps --filter "status=exited" -q)

It will delete all the stopped containers .

But how did it worked ?

docker rm command needs IDs or names of the containers to be deleted. But if we want n number of stopped or exited containers to be deleted then it is difficult to pass all the container IDs in a single command. Therefore, to make our life easy first we fetched the IDs of all stopped containers using following command,

docker ps --filter "status=exited" -q

Here we filtered the containers whose status is exited and using -q option got their Ids only. So, it returned the Ids of all stopped containers i.e.

4622f48ceaed
ecc42527bba4
6f5190548f7a
6708084317a1
af70224e7cc5
121ba587209b

Then we passed these list of exited / stopped container IDs to docker rm command i.e.

docker rm $(docker ps --filter "status=exited" -q)

This is how it removed all stopped containers.

Using the same logic of filtering we can delete the containers in other state too. A container can be in following states i.e. created , restarting , running , removing , paused, exited, dead.

To delete the containers in any specific state, pass that to filter as we did above.

How to Remove all Docker Containers ?

Well there might be situation when we want to delete all the containers i.e. either they are running or stopped or in any other state. For that we can use the following command,

docker rm $(docker ps -a -q)

It will basically delete all the containers (both running and stopped).

How to remove docker containers by Image Name

Suppose we have n number of docker containers (both running & stopped). Now we want to delete all the containers by image name.
For that we need to filter the containers by image name, then pass their IDs to docker rm command i.e.

docker rm -f $(docker ps -a --filter="ancestor=centos")

It will delete all the containers of image ‘centos’.

How did it worked ?

We filtered the containers using image name i.e.

docker ps -a --filter="ancestor=centos"

It returned a list of container IDs (both running and stopped) whose image was ‘centos’ i.e.

65a46390e2df
a76e96d61e1f
0901ba9def92
66fab37f25f7
4e88ee8e736e

Now we passed this to docker rm command to delete this containers.

Remove containers using multiple filters

Suppose we want to delete all the containers which are stopped i.e. in exited state and were created using image ‘ubuntu’. Then we will use following command i.e.

docker rm $(docker ps -a --filter="ancestor=ubuntu" --filter "status=exited")

It will delete all the containers which are in exited state and were create from ubuntu image. Using this technique we can pass multiple filters while filtering containers for deletion.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top