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.
Solution 1:
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
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
Solution 2:
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
Solution 3:
Here is short and quick solution I used
Docker provides a single command that will clean up any resources — images, containers, volumes, and networks — that are dangling (not associated with a container):
docker system prune
To additionally remove any stopped containers and all unused images (not just dangling images), add the -a flag to the command:
docker system prune -a
For more details visit link
Solution 4:
docker image prune -a
Remove all unused images, not just dangling ones. Add
-f
option to force.
Local docker version: 17.09.0-ce, Git commit: afdb6d4, OS/Arch: darwin/amd64
$ docker image prune -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker image prune [OPTIONS]
Remove unused images
Options:
-a, --all Remove all unused images, not just dangling ones
--filter filter Provide filter values (e.g. 'until=<timestamp>')
-f, --force Do not prompt for confirmation
--help Print usage
Solution 5:
Easy and handy commands
To delete all images
docker rmi $(docker images -a)
To delete containers which are in exited state
docker rm $(docker ps -a -f status=exited -q)
To delete containers which are in created state
docker rm $(docker ps -a -f status=created -q)
NOTE: Remove all the containers then remove the images