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.

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,

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,

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

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

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.

Now to delete all the stopped containers use following command,

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,

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.

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

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,

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.

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

How did it worked ?

We filtered the containers using image name i.e.

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

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.

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.

Join LinkedIn Group of Python Professional Developers who wish to expand their network and share ideas.

You can also follow us On Twitter :

Click Here to Subscribe for more Articles / Tutorials like this.