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

or

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).

Top comments (1)

Collapse
 
wrijugh profile image
Wriju's Blog

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