BLACK FRIDAY SALE IS LIVE !!!!!

Need Assistance?

In only two hours, with an average response time of 15 minutes, our expert will have your problem sorted out.

Server Trouble?

For a single, all-inclusive fee, we guarantee the continuous reliability, safety, and blazing speed of your servers.

How To Remove Exited Containers In Docker?

Considering removing exited containers in Docker? Our Docker Support team has the answer. Now let’s determine what those are.

Removing Exited Containers in Docker

Docker makes it simple to collect an excessive number of exited containers, which clutter the output and utilize disc space. Docker also offers command-line tools for system cleanup.

If we know beforehand that a container will not be around for long, we can use the command below to delete it when it exits:

docker run --rm image_name

Additionally, we can use docker ps -a to locate containers and filter them according to their status: created, restarting, running, paused, or exited. Utilize the -f flag to filter the list of exited containers according to their status. When we are certain that we want to remove these containers, we can pass their IDs to the docker rm command with the -q flag.

To obtain a list of the exited containers, enter the following command:

docker ps -a -f status=exited

Afterward, we can use the following command to remove the exited containers:

docker rm $(docker ps -a -f status=exited -f status=created -q)

Additional commands for removing exited containers include:

We can use any of the following commands 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 ps -a |findstr Exited) | foreach-object {docker rm $_.split(" ")[-1]}

Since Docker 1.13.x, Docker container prune can be used to remove all stopped containers. This will function identically across all platforms. The following is the command:

$ 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

There is also a Docker system prune that removes unused containers with a single command:

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

For older versions of Docker, it is possible to combine it with other Unix commands:

$ docker ps --filter "status=exited" | grep 'weeks ago' | awk '{print $1}' | xargs --no-run-if-empty docker rm

The following command will remove all containers:

docker rm `docker ps --no-trunc -aq`

For Windows, this will be the command:

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

For PowerShell, this will be the command:

docker rm @(docker ps -aq)

Summary

Docker simplifies the containerization of applications and services, allowing them to run anywhere. It is easy to accumulate an excessive number of unutilized images, containers, and data volumes when working with Docker, which clutters the output and utilizes disc space. This article describes a variety of Docker container removal methods.

Liked!! Share the post.

Get Support right now!

Start server management with our 24x7 monitoring and active support team

Can't get what you are looking for?

Available 24x7 for emergency support.