How to remove all containers in docker?

How to remove all containers in docker?

You have probably heard about Docker at some time in your professional career as a developer. And as you are probably aware, it has become a crucial piece of technology for any application developer to understand. Docker lets us manage your infrastructure in the same manner that we manage our applications. If you’re just learning Docker and you’re creating containers to learn how it works, chances are at some point you’d want to remove the containers. It can also happen that you’ve made some sort of mistake and simply want to start over. In this article, we will first briefly look at what docker is, and how we can remove all containers in docker with the help of a few commands.

What is Docker?

Before we get to the main part, let us try to briefly discuss what docker is. With the help of Docker, you may package and operate an application inside a loosely isolated environment known as a container. You can execute numerous containers concurrently on a single host thanks to the isolation and security. You can run applications without depending on what is already installed on the host because containers are lightweight and come with everything you need to run them. Sharing containers while working is simple, and you can make sure that everyone you share with receives the same container that operates in the same way.

Docker also provides a platform and great tooling to help you manage the lifespan of your containers. You can use containers to create your application and the components that support it. Your application is then distributed and tested as a unit using the container. When you’re ready, deploy your application as a container or an orchestrated service into your production environment. Whether your production environment is a local data center, a cloud service provider, or a combination of the two, this still functions.

There is so much to learn and love about Docker. If you’d like to learn more, the official getting started guide is a great place to start.

Remove all containers in Docker

Now that we have briefly discussed Docker, let us look at how we can remove all the containers in Docker.

Let us first list all the containers so we know what we are dealing with. You can do so with the following command,

docker container ls -a
Code language: Bash (bash)

It will list all containers, whether it is running or not.

Next up, we would want to stop the running containers before removing them. Run the following command to stop all running containers,

docker stop $(docker ps -aq)
Code language: Bash (bash)

Now we can safely remove all the containers. We must obtain each container’s id in order to remove every container from the Docker machine. With the use of the command docker ps -aq, we can easily obtain the ids of the containers. We can then use the docker rm command to delete all of the containers from the Docker machine. The command should look like this,

docker rm $(docker ps -aq)
Code language: Bash (bash)

Optionally, to clear up extra space and clutter in the system, we can remove all images from the Docker machine. We can still retrieve the most recent version or a specific versioned image from the cache or the Docker registry. Use the following command to remove all the images,

docker rmi $(docker images -q)
Code language: Bash (bash)

And we are done! How simple was that? In some cases, you may not want to remove all the containers at once. In that scenario, you can apply different filters to all the commands we ran before using the --filter flag. For example, you can remove all the containers which are stopped using the command below,

docker rm $(docker ps --filter status=exited -q)
Code language: Bash (bash)

Notice how I’ve used the filter status=exited. There are many more key-value pairs available to use as filters, and you can also use any other filters here according to your needs. You can see the full list of available filters in the official docs.

You can of course use filters with other commands as well like so,

docker stop $(docker ps --filter status=running -q)
Code language: Bash (bash)

This command will stop all the containers which have the status of “running”. By now you should have a good idea of how these commands work, and you should be able to use them for your needs.


And that is a wrap. Docker is a tool that almost every developer comes across at some point in their career. With this useful tool, we can package and operate an application inside isolated environments or containers. But sometimes you just want to start over or remove the numerous containers you had created for learning. In this article, we looked at what Docker is, and all the commands needed to remove all the containers from your system. You should also be able to customize these commands to fit your needs.

If you have any questions regarding this article or want to talk about anything technology, you can find me on Twitter. Thank you for reading!

Sharing is caring

Did you like what Supantha Paul wrote? Thank them for their work by sharing it on social media.


No comments so far