Docker Tips: How to Delete All Docker Containers and Images
It's time to start sharing some tips I've found along the way while using Docker. You may come upon a time where you will be required to clean up your Docker installation.
If you have used docker for any length of time you will start to notice unused containers and images laying around. Sometimes this can become a burden on your disk space or just an administrative hassle. Either way it is a quick and easy fix.
Before proceeding please be aware that these commands will nuke every non-running container and image that is not associated to a running container. You've been warned.
Delete all Docker containers that are not running
root@Docker01:~# docker rm $(docker ps -a -q)
Delete all Docker Images
I've provide 2 different options for deleting images. To correctly understand the difference between Tagged and UnTagged Images. Please take a look at the diagram below.
Option 1 - Delete all untagged Docker Images This was updated based on Matthew Torres's comment below as it is much easier to delete with the filtering flag Check out Docker's Filtering Docs for more information.
docker rmi $(docker images -f "dangling=true" -q)
Option 2 - Delete all Docker Images
docker rmi $(docker images -q)
Jan responded on Twitter with the following proposal which works also quite well. I teste this and works well.
@docker @idomyowntricks use formatting instead of trying to remove all incl. running ones (-qa): docker rm $(docker ps -qf status=exited)— Jan (@KekSfabrik_EU) February 23, 2016
Option 3 - Delete all Docker Containers according to formatting
docker rm $(docker ps -qf status=exited)
If you liked this article be sure to Follow Me on Twitter to stay updated!