Home > Software Development > Simple Script to List and Remove All Stopped Docker Containers

Simple Script to List and Remove All Stopped Docker Containers

While using Docker command lines, I found it’s sometime a bit confusing to deal with containers. For the underlying operating system, a container is like a process. That is why the docker command use “ps” as the command to list these commands.

However, the typical “ps” command lists all the running processes. When a process dies, it is gone and there is no need to list it at all. For the containers, the story is different. The container can also be stopped, paused, which is different from running state. But you can still bring them back to running state later.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

To list all the containers, the “docker ps” command is used. By default, it list only those running containers. While it’s perfectly fine, it makes lots of people wonder how to find out these stopped or paused containers. Unless they are removed by the “docker rm” command, they still use some system resources.

The “docker ps” is actually capable of listing all the container regardless their states. It needs the “-a” option as follows. Without the option, you won’t see any stopped containers. That is why sometimes some systems end up with many stopped containers that users are not aware of.

# docker ps -a

To further filter the containers based on their states, you can use either Linux command, for example, grep with a certain keyword. In the latest version, you can use the “–filter” option. The following simple command can list only these already stopped containers:

# docker ps -a —filter="status=exited"

The command will list container id, status, etc for each stopped containers. To remove all of them, we need a bit of Shell scripting (not too complicated though).

To remove one container is quite easy with the “docker rm” command followed by the container ID. All these container IDs can be retrieved from the above “docker ps” command. The awk command can be handy to get these container IDs:

# docker ps -a —filter="status=exited” | awk ‘{if(NR)>1 print $1}'

Note that the NR > 1 is to skip the table header of the output. The output of the above command are container IDs each one which is on its own line.

The next thing is to send all the IDs to the “docker rm” command. The xargs command can be a big help here. That completes the whole script to remove all the stopped containers:

# docker ps -a —filter="status=exited” | awk ‘{if(NR)>1 print $1}’ | xargs docker rm

After writing the above, I learned another a bit shorter form of command:

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

The -q option means quiet mode that displays only the numeric IDs, which essentially does what early awk command does.

  1. May 24th, 2016 at 10:38 | #1

    in docker 1.9 I have to use –filter . -filter does not work

  2. May 24th, 2016 at 12:27 | #2

    Thanks for the info Has!

  3. Paul
    August 28th, 2016 at 04:56 | #3

    The filter arguments needs a double dash. WordPress is converting it to a long dash, so beware. So, it’s minus minus filter.

  4. D
    January 17th, 2017 at 07:11 | #4


    docker ps -a -qf status=stopped | xargs -r docker rm

    You can also clean your dangling images:

    docker images -qf dangling=true | xargs -r docker rmi

    Both are reasonably safe to use in cronjobs, xargs -r will not run if the input is empty.

  5. January 17th, 2017 at 21:49 | #5

    Thanks Alex for sharing the command lines!

  6. March 3rd, 2017 at 14:29 | #6

    Fabulous, what a webpage it is! This blog presents helpful information to us, keep it up.

  1. No trackbacks yet.