Docker Community Forums

Share and learn in the Docker community.

Command to remove all unused images


(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 : https://github.com/CloudSlang/cloud-slang

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


(Meir Wahnon) #14

A more elaborate tutorial on this in DigitalOcean blog : https://www.digitalocean.com/community/tutorials/how-to-clean-up-your-docker-environment-using-cloudslang-on-a-coreos-cluster-2


(Ryan Trauntvein) #15

I like to use this script from Spotify: https://github.com/spotify/docker-gc

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 https://github.com/yelp/docker-custodian


(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! https://github.com/ZZROTDesign/docker-clean


(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


(Tobias Munk) #22

See also https://github.com/docker/docker/pull/26108 New Data Management commands


(Sam Saffron) #23

Super duper awesome, such a welcome addition to Docker.

docker system prune

or

docker image prune

should do the trick next version!


#24

If someone uses earlier version

docker rmi $(docker images -q -f "dangling=true")

(Chrisob55) #25

Thanks. Very helpful!


(Vsupalov) #26

Cleaning up after Docker right now is as easy as issuing

docker system prune

This works starting with version 1.25, you can read more here.

Previously, or if you prefer to have a bit more control, you could use the following:

$ docker rm -v $(docker ps -aq -f 'status=exited')
$ docker rmi $(docker images -aq -f 'dangling=true')

# ATTENTION: this will also remove volumes of docker-compose if the containers are barely stopped
$ docker volume rm $(docker volume ls -q -f 'dangling=true')

If you want more details about cleaning up after Docker, check out this article.


(Mundimduy2506) #27

I used these to clean images

sudo docker rmi -f $(sudo docker images | grep “” | awk “{print $3}”)


(Curtisrcooley) #28

I think this is similar

docker images -f "dangling=true" --format "{{.ID}}" | xargs docker rmi -