Remove containers created before an other container docker rm $(docker ps -before 9c49c11c8d21 -q)Īnother example. Determine the ID of the working container with docker ps. You want to remove containers created before that working version. Let's say You have developed your system, now it is working but there are a number of containers left. This feature is at least in docker version 0.6.5. If you do not like to remove all containers, you can select all containers created before or after a specific container with docker ps -before or with docker ps -since. Thanks to Adrian (and Ryan's answer) for mentioning the new ps filtering. Updated to answer both, including new links to documentation. Here's a cleaner script to add in your ~/.bashrc or ~/.profile : # Use `docker-cleanup -dry-run` to see what would be deleted.ĭANGLING=$(docker images -q -f "dangling=true")Įdit: As noted below, original answer was for removing images, not containers. The older docker rm $(docker ps -aq) trick was even worse as it tried to remove any running container, failing at each one. However, any of those will cause docker rm or docker rmi to throw an error when there are no matching containers. It is now possible to use filtering with docker ps: docker rm $(docker ps -q -f status=exited)Īnd for images: docker rmi $(docker images -q -f "dangling=true")