I’ve been playing with Docker off and on for almost a year and noticed that I have a lot of abandoned images on my system that are left over from experimenting.  This post contains a few commands you can use to clean up your system.

To illustrate what I mean by “abandoned images”, here’s the output of `docker images` on my machine.

That last image is 11 months old and at this point I have no idea what the hell I was doing with it — time to clean!

The list below is a (mostly) plagiarized repost of an answer left by Ulises Reyes on Stack Overflow and explains how to clean up old images.


Delete One Image


docker rmi <the_image_id>


Delete All Images

The `docker rmi` command can accept a list of image IDs so destroying all images is pretty easy:

docker rmi $(docker images -q)


Stop and Delete All Images

Gracefully (SIGTERM) stop all images then delete them.

docker rm $(docker stop $(docker ps -q))


KILL and Delete All Images

SIGKILL all images then delete them.

docker rm $(docker kill $(docker ps -q))


Delete All Images Except for…

If you want to delete all images except for a select few it can be done using grep.  Basically you will make a call to `docker images` then filter the list using grep.

The example below will delete all images that do NOT have contain the words “dont”, “kill” and “me”.

Note — the `awk {‘print $3}` is what actually prints out the image ID.

docker rmi $(docker images | grep -v 'dont\|kill\|me' | awk {'print $3'})

Here’s a nice snippet that can be used in a PostgreSQL database to output the current database’s size.

SELECT pg_size_pretty(pg_database_size(current_database())) AS human_size, 
       pg_database_size(current_database())                 AS raw_size;

That statement with output something along the lines of:

human_size  | raw_size
181 GB      | 193841573064