How to remove old Docker containers?


Docker allows you to remove old and stale containers that are of no use to you. You can use the Docker rm or Docker container rm commands to do so. However, before you start removing your containers, you need to make sure that none of your containers are actively running. In such a case, Docker will throw an error.

However, there is another workaround to this. You can remove Docker containers forcefully using the --force option as well. The Docker remove commands allow you to remove one or more Docker containers together. You just need to specify the container IDs or their names in order to do so. However, if you want to delete all the Docker containers in your machine simultaneously, you can do so by using a sub-command.

In this article, we will discuss how to use the Docker container remove commands and all the options that you can mention along with it. We will also discuss how to remove all containers simultaneously.

Docker Container Remove

You can use two different forms of the Docker container remove command to achieve the same result. Following are the syntax of these two commands.

$ docker container rm [OPTIONS] CONTAINER [CONTAINER...]


$ docker rm [OPTIONS] CONTAINER [CONTAINER...]

Both these commands work the same way. The options that you can use along with these commands are -

  • --volumes - If you want to remove all the associated volumes as well, you can use this option.
  • --link - If the container that you are trying to remove has links to other containers and you want to delete those links as well, you can use the --link option.
  • --force - If the container that you are going to remove is actively running in the background, you can use the --force option to remove such containers without even stopping them.

Now, let’s check out a few examples and ways in which you can use these commands and options.

Suppose you have a container called mycont actively running in your machine. Then the conventional way to remove this container would be to first check its status, then stop the container if it's running, and then remove it. To do so, you can use the following set of commands.

To list all active containers, you can use the following command.

$ docker ps

Now, if your container is in an active state, you need to first stop the container. You can do so using the following command.

$ docker stop mycont

Once you have stopped this container, you can easily remove it using either of the following commands.

$ docker container rm mycont


$ docker rm mycont

If you don’t want to go through all these hassles, you can directly remove the container forcefully using the command below.

$ docker rm -f mycont

This will not throw an error even if you are trying to remove a container that is actively running.

You can remove more than one container simultaneously by mentioning the container ID or names separated by spaces. Check out the command below to remove 3 containers together.

$ docker container rm mycont1 mycont2 mycont3

You can also use a mix of two commands together to remove all the containers in your system at once. Check out the command below.

$ docker stop $(docker ps -aq)

In the command above, we have used the Docker stop command as a parent command and instead of mentioning the ID or name of each individual container, we have used a subcommand that lists the container IDs of all the containers. The -q (quiet) option is used to return only the container ID and -a (all) option is used to return all the containers.

Now that you have stopped all the containers, you can use a similar command to remove all the containers.

$ docker rm $(docker ps -aq)

Instead of first stopping all the containers and then removing them, you can use the -f option here as well, to remove all the containers together forcefully.

$ docker rm -f $(docker ps -aq)

In case you want to remove only some stopped containers, you can use the filter option to provide the status of the container.

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

In the above command, we have used the Docker rm command along with a sub-command that filters all the exited containers and returns their IDs.

To sum up, in this article, we discussed how to remove one or more containers using two different commands. We saw how to use multiple options along with them and how to remove or stop all containers simultaneously.

Updated on: 06-Aug-2021

528 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements