DEV Community

GaMa
GaMa

Posted on • Updated on

How to remove temporal untagged Docker images

*Update, thanks to champkeh

The correct way to remove unused docker images is using:

docker image prune [OPTIONS]

For more information you can go to:

https://docs.docker.com/engine/reference/commandline/image_prune/


If you have some experience with Docker you will know that mistakes are made while building your own images. At the beginning I didn't cared to delete one untagged image from time to time by using the docker rmi ###### command, however, when I had to work on a project to migrate a legacy application to Docker, I realized that there are a LOT of images just hanging out in the ether like "we are here just chillin".

chillin

So, I started doing some cleanup one by one, until, you know, who wants to paste the image id or the name to delete it? Right. Then I came up with the following one-liner:

docker images | awk '{if(match($1,"<none>")){print "docker rmi " $3 ";" | "/bin/sh"}}'

You can then add the line to a shell script or whatever.The good thing is that if by some reason an image has a dependen child (container associated to it) it won't delete it unless you force it (-f).

Top comments (2)

Collapse
 
champkeh profile image
champ • Edited

why try this:

docker image prune

or

docker image prune -f
Collapse
 
ech0server profile image
GaMa

This is awesome, did not know it existed, thank you I will update my post !! :)