Docker Community Forums

Share and learn in the Docker community.

Command to remove all unused images

(Sam Saffron) #1

Is there a shortcut rmi command to delete all images that are currently not in use, or cool shell script I could use?

We are accumulating tons of images during deploys and I need to keeps stuff squeeky on the prod boxes.

(Thatcher Peskens) #2

My favorite way of removing all stopped docker containers is:

docker ps -q |xargs docker rm 

it will list all images (docker ps), but only show the id’s. And then run a docker rm command for each one of them.

Similarly for removing all unused docker images

docker images -q |xargs docker rmi

Will list all docker images and then docker rmi them.

(Sven Dowideit) #3

nooooooooo! :smile:

docker rmi $(docker ps -q) 

(Sam Saffron) #4

My main issue is dealing with orphan images.

docker rmi `docker images | awk '{ print $3; }'`

Does the trick… however it feels like I am playing with fire and it raises lots of errors.

(Damien Coraboeuf) #5


I’m using:

docker rmi -f $(docker images | grep "<none>" | awk "{print \$3}")

in order to get rid of all untagged images.


(Scott Hansen) #6

Other ways to skin the proverbial cat…For images:

alias diclean='docker images | grep '\''<none>'\'' | grep -P '\''[1234567890abcdef]{12}'\'' -o | xargs -L1 docker rmi'

For containers (my data-only containers and named running containers all end in _run, _data or _config):

alias dclean='docker ps -a | grep -v '\''CONTAINER\|_config\|_data\|_run'\'' | cut -c-12 | xargs docker rm'

The ugly escaping/quoting is to make the alias work correctly. You won’t need as much just running the commands directly.


(Sam Saffron) #7

Appreciate it, my concern though is that there is no “tree of images” aware mechanism out there. It would be super nice if you could just docker rm all the containers no longer in use and then, docker rmi --unused to clean everything that is left up.

Raised a feature req here:

(Sven Dowideit) #8

and as an example of why simple things have not been coded yet

(Ecardoso) #10

I wrote a shell script to remove exited containers, untagged images and images not associated to any container, hope it helps.

(Meir Wahnon) #11

I was part of creating a tool for this issue, hope it brings value . here is an example video on how to use it in a coreos cluster:

GitHub repo (tool is generic, this is one of the use-cases):

(Sam Saffron) #12

Where is the actual code that does the cleanup?

(Meir Wahnon) #13

So i’ts a workflow engine, it has it’s own DSL (Yaml based , like Docker compose)…
The workflow engine codebase is here :

The Yaml based “flows” files are here (look for “” file as the parent flow) :

(Meir Wahnon) #14

A more elaborate tutorial on this in DigitalOcean blog :

(Ryan Trauntvein) #15

I like to use this script from Spotify:

From the README:

A simple Docker container and image garbage collection script.

  • Containers that exited more than an hour ago are removed.
  • Images that don’t belong to any remaining container after that are removed.

Although docker normally prevents removal of images that are in use by containers, we take extra care to not remove any image tags (e.g., ubuntu:14.04, busybox, etc) that are in use by containers. A naive docker rmi $(docker images -q) will leave images stripped of all tags, forcing docker to re-pull the repositories when starting new containers even though the images themselves are still on disk.

(Sam Saffron) #16

Awesome, @solomon / @jess is this script’s functionality something you would consider including in Docker proper?

(Dnephin) #17

Another implementation of the same docker-gc idea is

(Sean Kilgarriff) #18

I, along with one of my good friends, built a nice wrapper for some common Docker cleanup commands, I would love for you to check it out and see if it helps!

(Kebabra Abdessamad) #19

thanks that was helpful

(Jaydipdave) #20

Here is the final answer:

docker images | grep " [months|days|weeks]* ago" | awk '{print $3}' | xargs docker rmi

(Vadorequest) #21

This version is more powerful/dangerous, use with caution: (only use it if you don’t care, because it’s likely to remove everything)
docker images -a | grep " [hours|months|days|weeks]* ago" | awk '{print $3}' | xargs docker rmi -f