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.

This entry was posted in Software Development and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Posted May 24, 2016 at 10:38 am | Permalink

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

  2. Posted May 24, 2016 at 12:27 pm | Permalink

    Thanks for the info Has!

  3. Paul
    Posted August 28, 2016 at 4:56 am | Permalink

    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
    Posted January 17, 2017 at 7:11 am | Permalink


    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. Posted January 17, 2017 at 9:49 pm | Permalink

    Thanks Alex for sharing the command lines!

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.