How to Remove Exited Containers in Docker

How to Remove Exited Containers in Docker

Docker makes it easy to collect an excessive number of discarded containers, which block output and take up disc space. Additionally, Docker provides command-line utilities for system cleanup.

When a container quits, we can use the command shown below to delete it if we know in advance that it won’t be around for long:
docker run --rm image_name

Furthermore, we may find containers using docker ps -a and filter them based on their status, such as created, resuming, running, paused, or exited. The list of exited containers can be filtered by status with the -f parameter. We may send these containers’ IDs to the docker rm command with the -q argument after we are convinced we want to remove them.

Enter the next command to get a list of the exited containers:
docker ps -a -f status=exited

In order to remove the exited containers, we can use the following command:
docker rm $(docker ps -a -f status=exited -f status=created -q)

For removing exited containers, further commands include:

Any of the commands listed below can be used to remove all containers that have exited:
sudo docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs sudo docker rm
$ docker rm $(docker ps -a -q -f status=exited)$ docker rm $(docker ps -a -q -f status=exited)
(docker ps -a |findstr Exited) | foreach-object {docker rm $_.split(" ")[-1]}

Since Docker 1.13.x, all stopped containers can be deleted using the Docker container prune tool. This will work the same way on all platforms. The order is as follows:

$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
4a9f7edbae0f63178aff7eb0aa39f0627a203ab2df258c1a00b456cf20063
f98f9c2aa1eaf727e4ea9c0283bcaa4762fbdba7f36191f26c97f64090361 Total reclaimed space: 212 B

With a single command in the Docker system, prune eliminates all unnecessary containers:

docker system prune -a

WARNING! This will remove:
– all stopped containers
– all volumes not used by at least one container
– all images without at least one container associated to them
Are you sure you want to continue? [y/N] y

It is possible to integrate Docker with other Unix commands for earlier versions of the software:
$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

All containers will be removed with the following command:
docker rm `docker ps --no-trunc -aq`

For Windows, use the following command:
FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

For PowerShell, use the following command:
docker rm @(docker ps -aq)

Applications and services can now operate everywhere thanks to Docker’s simplified containerization process. When working with Docker, it is simple to create an excessive amount of unused images, containers, and data volumes, which clogs the output and takes up disc space. Several Docker container removal techniques are covered in this article.

Was this article helpful to you? If you continue having problems with the above outlined, please contact the eTechSupport Team.

Follow us on FacebookTwitter, and LinkedIn for additional information.


Get your free trial now