DEV Community

Wriju's Blog
Wriju's Blog

Posted on

How to delete all Docker containers using a single Command

There may be a many containers either running or exited in your development machine. You need to clean them up in a regular basis.

To see the list of docker container running locally,

docker ps
Enter fullscreen mode Exit fullscreen mode

But it does not show the excited containers. To see them all use -a

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Finally to delete you need to pass either the id or the name of each container. Wouldn't it be nice to delete them in a loop (its the concept here but we are not using it)? YES indeed it would be. To get only the ids of all the containers we can use -q switch.

So to get the list of only ids for all the containers we can use,

docker ps -a -q 
Enter fullscreen mode Exit fullscreen mode


docker ps -aq
Enter fullscreen mode Exit fullscreen mode

To delete we use

docker rm container_id
Enter fullscreen mode Exit fullscreen mode

If we want to loop (again not actual loop but a concept) through all

docker rm $(docker ps -aq)
Enter fullscreen mode Exit fullscreen mode

That's it - your magic command to delete all the running and exited containers in your machine.

This works in Linux (not in Windows).

Discussion (2)

codingsafari profile image
Nico Braun • Edited on

It's nice that you show that but it would be better to explain the background of how it works. Magic commands to memorize are not good. Providing long flag names for clarity is also a good habit when writing guides. Although I'm guilty of using short forms at times too.

This isn't deleting in a loop. It makes use of regular shell syntax / constructs. If you understand those, you can build more magic commands easily and they aren't all that magic anymore.

docker rm can take a list of container names or ids and deletes all of them. This fact you pair with command substitution, $(). First the expression in those round brackets is evaluated and substituted in the parent command. You can do that for many things that is not unique to docker. We see it actually a lot for variable assignment, but also for other things.

In your case, you evaluate docker ps inside. The problem with that is that it's output is rather verbose. It would be nice, if it were more quiet, hence the --quiet or short -q flag. With that flag you only get the id(s) of the container(s). Now the result will be that you substitute that command for a list of IDs in the docker rm command. Thus docker goes ahead and deleted all of them.

It's also important to note that in this case you should not put double quote around the command substitution as it's more often than not the case "$()". Actually, tools like shellcheck will mark it as warning, if no quotes are provided. The quotes would result in the full list being used as a single value, which wouldn't work with docker rm as it expects and actual list and not a single value.

Now with all that knowledge, lets try to make another magic command. Be careful this will remove all your local images.

docker rmi $(docker image ls -q)
Enter fullscreen mode Exit fullscreen mode
wrijugh profile image
Wriju's Blog Author

I liked your explanation. Learned few things. Many thanks.