Docker: remove all Exited containers
A oneliner!
sudo docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs sudo docker rm
Written by Filippo Valsorda
Related protips
9 Responses
Love the simplicity of this :)
docker rm $(docker ps -q -f status=exited)
docker rm $(docker ps --all -q -f status=exited)
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.
Prefer using miraculixx command
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
IMHO
docker rm $(docker ps -qa --filter status=exited )
docker rm `docker ps -a | grep "Exited " | awk '{print $1}'
`
why don't use container prune to remove all exited containers?
$ docker container prune