Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: How to delete all local Docker images

I recently started using Docker and never realized that I should use docker-compose down instead of ctrl-c or docker-compose stop to get rid of my experiments. I now have a large number of unneeded docker images locally.

Is there a flag I can run to delete all the local docker images & containers?

Something like docker rmi --all --force --all flag does not exist but I am looking for something with similar idea.

like image 354
Kimmo Hintikka Avatar asked Jun 27 '17 16:06

Kimmo Hintikka


People also ask

How do I delete all docker images locally?

Remove all images All the Docker images on a system can be listed by adding -a to the docker images command. Once you're sure you want to delete them all, you can add the -q flag to pass the image ID to docker rmi : List: docker images -a.


2 Answers

For Unix

To delete all containers including its volumes use,

docker rm -vf $(docker ps -aq) 

To delete all the images,

docker rmi -f $(docker images -aq) 

Remember, you should remove all the containers before removing all the images from which those containers were created.

For Windows (power shell, not cmd)

In case you are working on Windows (Powershell),

$images = docker images -a -q foreach ($image in $images) { docker image rm $image -f } 

Based on the comment from CodeSix, one liner for Windows Powershell,

docker images -a -q | % { docker image rm $_ -f } 

For Windows using command line,

for /F %i in ('docker images -a -q') do docker rmi -f %i 
like image 150
techtabu Avatar answered Sep 20 '22 20:09

techtabu


Use this to delete everything:

docker system prune -a --volumes 

Remove all unused containers, volumes, networks and images

WARNING! This will remove:     - all stopped containers     - all networks not used by at least one container     - all volumes not used by at least one container     - all images without at least one container associated to them     - all build cache 

https://docs.docker.com/engine/reference/commandline/system_prune/#extended-description

like image 38
Robert Avatar answered Sep 20 '22 20:09

Robert