In my day to day testing with Docker, I end up with a ton of containers, images and volumes. For testing purposes, I sometimes need a clean slate and clear out absolutely everything. Below are some of the commands that have made that process very easy.
Remove all containers:
$ docker rm $(docker ps -qa)
The command ‘docker ps -qa’ feeds in just the container ID for all running and non-running containers.
To remove all containers and their attached volumes, use the -v option. This will prevent orphaned volumes.
$ docker rm -v $(docker ps -qa)
Remove all images:
$ docker rmi $(docker images -q)
Remove all volumes:
$ docker volume rm $(docker volume ls -q)
Want to get fancy and only remove a group of containers based off their name.
Remove containers by name:
$ docker rm -v $(docker ps -a | awk '{ print $1 }' | grep magento)
Remove images by name:
docker rmi $(docker images -a | awk '{ print $1,$2,$3 }' | grep magento | awk '{print $3 }')