Command to remove all unused images

nooooooooo! :smile:

docker rmi $(docker ps -q) 
1 Like

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.


I’m using:

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

in order to get rid of all untagged images.



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.


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:

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

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

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):

1 Like

Where is the actual code that does the cleanup?

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) :

A more elaborate tutorial on this in DigitalOcean blog :

I like to use this script from Spotify: GitHub - spotify/docker-gc: INACTIVE: Docker garbage collection of containers and images

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.


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

Another implementation of the same docker-gc idea is

1 Like

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!

thanks that was helpful

Here is the final answer:

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

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

See also New Data Management commands

1 Like

Super duper awesome, such a welcome addition to Docker.

docker system prune


docker image prune

should do the trick next version!