Last Updated: September 30, 2021
·
182K
· filosottile

Docker: remove all Exited containers

A oneliner!

sudo docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs sudo docker rm

9 Responses
Add your response

Love the simplicity of this :)

over 1 year ago ·

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

over 1 year ago ·

@vosmith

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

over 1 year ago ·

@miraculixx

I don't think --all can be used with -f. Usually --all would display stopped/exited containers, but with -f status=exited, it already returns those. Please correct me if I'm wrong.

over 1 year ago ·

Prefer using miraculixx command

over 1 year ago ·

This change is a little more bulletproof, though I am generally a huge fan of using grep and cut, but when you are doing something that removes containers explicit is good.

This will avoid inadvertently removing any containers which happen to have the word Exit in the name or command, and won't stop working if the output format of "docker ps -a" ever changes.

docker ps -a --filter status=exited --format {{.ID}} | xargs docker rm

over 1 year ago ·

IMHO
docker rm $(docker ps -qa --filter status=exited )

over 1 year ago ·

docker rm `docker ps -a | grep "Exited " | awk '{print $1}'`

over 1 year ago ·

why don't use container prune to remove all exited containers?
$ docker container prune

over 1 year ago ·