Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to remove unused Docker containers and images

May 8, 2018

I wrote this four years ago, so instead use this command:

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

This comment has been minimized.

Copy link

lukewendling commented Dec 18, 2015

rm non-ascii chars

docker rmi $(docker images | grep "^<none>" | awk '{print $3}')

@quyen91

This comment has been minimized.

Copy link

quyen91 commented Dec 29, 2015

I have read it somewhere
It is working , special for deleting <none> images
$ docker-machine stop default
$ docker-machine start default
$ docker images -q | xargs docker rmi
or
$ docker-machine restart default
$ docker images -q | xargs docker rmi

@siripuramrk

This comment has been minimized.

Copy link

siripuramrk commented Jan 16, 2016

How to remove a container which has Image ID but not Name and Tag... ?

@siripuramrk

This comment has been minimized.

Copy link

siripuramrk commented Jan 16, 2016

This worked for me :) " $ docker rmi $(docker images -q -f dangling=true) "

@manolohk

This comment has been minimized.

Copy link

manolohk commented Mar 5, 2016

I recieved an error " No space left on device " since I used 100% of my iNodes. Running this command worked for me:

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

@jakthom

This comment has been minimized.

Copy link

jakthom commented Apr 1, 2016

Another way of removing all images is:

docker images -q | xargs docker rmi

If images have depended children, forced removal is via the -f flag:

docker images -q | xargs docker rmi -f

@jarek-przygodzki

This comment has been minimized.

Copy link

jarek-przygodzki commented May 5, 2016

I use this script

#!/bin/bash

# Delete all stopped containers
docker rm $( docker ps -q -f status=exited)
# Delete all dangling (unused) images
docker rmi $( docker images -q -f dangling=true)

Edit

xargs with --no-run-if-empty is even better as it does cleanly handle the case when there is nothing to be removed.

#!/bin/bash

# Delete all stopped containers
docker ps -q -f status=exited | xargs --no-run-if-empty docker rm
# Delete all dangling (unused) images
docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi
@rorono

This comment has been minimized.

Copy link

rorono commented May 6, 2016

+1

@Desperado

This comment has been minimized.

Copy link

Desperado commented May 30, 2016

jarek-przygodzki works for me :)

@HWiese1980

This comment has been minimized.

Copy link

HWiese1980 commented Jun 5, 2016

How would I do this on Windows? There's no xargs or $(...) syntax

@jarek-przygodzki

This comment has been minimized.

Copy link

jarek-przygodzki commented Jun 17, 2016

How would I do this on Windows? There's no xargs or $(...) syntax

It should work from Docker Quickstart Terminal created by Docker Toolbox installer.

@sangram-chavan

This comment has been minimized.

Copy link

sangram-chavan commented Jun 22, 2016

!/bin/bash

Delete all containers

docker rm $(docker ps -a -q)

Delete all images

docker rmi $(docker images -q)

@vishalvsh1

This comment has been minimized.

Copy link

vishalvsh1 commented Jun 29, 2016

docker rmi $(docker images -f "dangling=true" -q)
or
docker images | grep "weeks ago" | awk '{print $3}' | xargs docker rmi

or
docker ps -a | grep Exited | awk '{print $1}' | xargs docker rm

@luchsamapparat

This comment has been minimized.

Copy link

luchsamapparat commented Sep 27, 2016

@HWiese1980

FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"') DO docker rmi %i

@Azerothian

This comment has been minimized.

Copy link

Azerothian commented Nov 10, 2016

Tested on 1.12.3 (Windows and Linux (centos 7))

Linux

Containers

docker rm $(docker ps -a -q)

Images

docker rmi $(docker images -q)

Windows

Containers

FOR /f "tokens=*" %i IN ('docker ps -a -q') DO docker rm %i

Images

FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"') DO docker rmi %i

@ianmalott

This comment has been minimized.

Copy link

ianmalott commented Dec 7, 2016

As of 1.13.0, see the new prune commands:

docker container prune   # Remove all stopped containers
docker volume prune      # Remove all unused volumes
docker image prune       # Remove unused images
docker system prune      # All of the above, in this order: containers, volumes, images

Bonus:

docker system df         # Show docker disk usage, including space reclaimable by pruning

New Data Management commands PR: moby/moby#26108

@siddharthbarman

This comment has been minimized.

Copy link

siddharthbarman commented Dec 26, 2016

In Windows using PowerShell, this might work:
&'C:\Program Files\Docker\docker' ps -a | foreach-object { $l = $PSItem.ToString(); $c = $l.Substring(0, $l.IndexOf(" ")); if ($c.ToLower() -ne "container") { $cmd = "c:\Program Files\Docker\docker.exe"; & $cmd "rm" $c } }

@elutsky

This comment has been minimized.

Copy link

elutsky commented Jan 4, 2017

Worked great on the Windows container! Thanks!

@dermeister0

This comment has been minimized.

Copy link

dermeister0 commented Jan 12, 2017

PowerShell

docker ps -a -q | % { docker rm $_ }
docker images -q | % { docker rmi $_ }
@alexlopes

This comment has been minimized.

Copy link

alexlopes commented Mar 8, 2017

Great gist! Exactly what I was looking for

@rolf-d2i

This comment has been minimized.

Copy link

rolf-d2i commented Mar 15, 2017

If you dislike shell commands try installing docker for python with for instance pip install docker and use the following script to remove junk images

#!/usr/bin/python

import docker
dockDaemon = docker.from_env()

imgid = [I.id for I in dockDaemon.images.list() if I.attrs["RepoTags"]==[u'<none>:<none>']]

for id in imgid:
   try:
        print "Trying to remove image",id
        dockDaemon.images.remove(id)
        print "Removed image",id
    except docker.errors.APIError,e:
        print "skipping image ",id,"as this is still in use"
@joao-parana

This comment has been minimized.

Copy link

joao-parana commented Apr 24, 2017

@ianmalott list plus:

docker network prune
@yhnu

This comment has been minimized.

Copy link

yhnu commented May 10, 2017

mark

@kalakodumuri

This comment has been minimized.

Copy link

kalakodumuri commented May 24, 2017

I am using all those commands. I am getting an error while deleting an image which has dependent containers. I would like to know is there any way of deleting images which does not have any containers associated with it. Because I am running docker rmi command from jenkins job as docker rmi $(docker images -q). This command fails when removing an image which has dependent containers and due to that jenkins job is failing. Any suggestions on this.

@GerBreOwn

This comment has been minimized.

Copy link

GerBreOwn commented Jun 4, 2017

I have 3 ghost containers that I can't get rid of as they keep reappearing with another ID. I have tried all of the above examples even including one I found in a book which is: "docker update --restart=no <container_id or name>.
Any ideas on how I can make these go away???
Like a T-Shirt I saw "How can I miss you if you won't go away"
Thanks.

@GerBreOwn

This comment has been minimized.

Copy link

GerBreOwn commented Jun 4, 2017

Finally got rid of those ghosts. Did "docker update --restart=no <container_id or name> or list of containers" and then did a "systemctl restart docker" and then they were gone!!!

@GerBreOwn

This comment has been minimized.

Copy link

GerBreOwn commented Jun 4, 2017

I lied. 3 minutes after Docker restarted the 3 containers plus 2 more were back.

Does anyone know how I can completely get rid of those unwanted containers???

Thanks.

@88plug

This comment has been minimized.

Copy link

88plug commented Jun 21, 2017

@GerBreOwn

docker stop $(docker ps -a -q) &
docker update --restart=no $(docker ps -a -q) &
systemctl restart docker

@jarrekk

This comment has been minimized.

Copy link

jarrekk commented Jul 6, 2017

docker image prune

@ronyrun

This comment has been minimized.

Copy link

ronyrun commented Jul 11, 2017

Does anyone know if there is a Windows cheat sheet for all of those commands ?

@rutgerhofste

This comment has been minimized.

Copy link

rutgerhofste commented Jul 22, 2017

For Mac OSX I used:

docker rmi `docker images`
docker rm -f `docker ps -a`

Can anybody confirm / improve this command. It seems a bit tricky to me.

@pascalandy

This comment has been minimized.

Copy link

pascalandy commented Sep 11, 2017

On ubuntu 16, I run docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi but I'm getting few error I don't like:

Deleted: sha256:dcb6f5d567c4132f4921475010ac24b363aa69cb553de2a607a3000a5790eb73
Deleted: sha256:55050923de04598352fd3afcd08fb40a363d34528bd88a0b50403ad47eff1818
Untagged: abiosoft/caddy@sha256:9ddf5098c411437eb618f33046b13f2eb2a83e533e582053f0c2d90351b5e29d
Deleted: sha256:81d184abca3c6b789051b123ff5387ce1791b5a3ff363a1190f8bc65cd8ac4d0
Deleted: sha256:7dbeb84197d3d35e83e9d4bbb68e89816a5ed8f62072dc811432da332f4f6373
Deleted: sha256:6fa265aed859e111ca4e83441dce77b14a0692e3c295b72fb84ce696d3f81c4f
Deleted: sha256:ca8067d933cfd402d4f676775f4e623903ba42874fe47cce9694579c08644281
Deleted: sha256:a11b91f29e8caf4313f6f152a31bb63fc3f9ef25184837c4c163f783b4888663
Untagged: gliderlabs/logspout@sha256:9a807db8e9e484920c3228770da76b8af68e8704d8ca0965e68bf8cefc93cee1
Deleted: sha256:87895b8ba6144d395bc439523759a9b5637bf52a1007374fc2c24e1e0eba9067
Deleted: sha256:48f64bfe03edce8015597db7dc32f533538e3b45738af2f086f91ed8510a6299
Deleted: sha256:1e7daedfbf19ac38f824ab39a22c22502a9a6f80fec095b1cd12f37f1ca6b513
Deleted: sha256:e154057080f406372ebecadc0bfb5ff8a7982a0d13823bab1be5b86926c6f860
Error response from daemon: conflict: unable to delete edbfcbd9b2a3 (cannot be forced) - image is being used by running container c505416ead45
Error response from daemon: conflict: unable to delete 9b0dc474ee71 (cannot be forced) - image is being used by running container a0a5bd08a4ba
Error response from daemon: conflict: unable to delete c0daf5d22637 (cannot be forced) - image is being used by running container 6efb59171b5e
Error response from daemon: conflict: unable to delete a7a205723d12 (cannot be forced) - image is being used by running container 82bf86cc7464
Error response from daemon: conflict: unable to delete 47dbf4321bb4 (cannot be forced) - image is being used by running container f1d2bc973e98
Error response from daemon: conflict: unable to delete c68795cb48b2 (cannot be forced) - image is being used by running container 0293120cc87a
Error response from daemon: conflict: unable to delete 08e03914fceb (cannot be forced) - image is being used by running container 74c1e296ff6f
@gnanasundar

This comment has been minimized.

Copy link

gnanasundar commented Sep 17, 2017

I had to remove a group of containers with a particular label and the associated images.

This worked for me on windows powershell

docker ps -a -q -f "label=DEV" | foreach-object {docker stop $PSItem.toString()}
docker ps -a -q -f "label=DEV" | foreach-object {$i = docker inspect $PSItem.toString() --format='{{.Config.Image}}'; docker rmi -f $i}
docker ps -a -q -f "label=DEV" | foreach-object {docker rm -f $PSItem.toString()}

For linux:

docker stop $(docker ps -a -q -f "label=DEV")
docker rmi -f $(docker ps -a -q -f "label=DEV" | xargs docker inspect --format='{{.Config.Image}}')
docker rm -f $(docker ps -a -q -f "label=DEV")
@joao-parana

This comment has been minimized.

Copy link

joao-parana commented Sep 29, 2017

I put this alias in my .profile

alias docker-prune='docker system df && docker container prune && docker volume prune && docker system prune && docker system df'

Now, I can run docker-prune to clean-up all my Docker unused resources and reclaim disk space.

@LunaticZorr

This comment has been minimized.

Copy link

LunaticZorr commented Nov 30, 2017

Stop all containers:
for i in $(docker ps | awk '{print $1}' | awk '{if(NR>1)print}'); do docker stop $i; done

Delete all images:
for i in $(docker images | awk '{print $3}' | awk '{if(NR>1)print}'); do docker rmi $i; done

@friscochap

This comment has been minimized.

Copy link

friscochap commented Dec 26, 2017

Remove all docker images marked as "none" in Powershell

docker rmi ((docker images | select-string "<none>" | ConvertFrom-String) | Select -ExpandProperty "P3")

@rpratesh

This comment has been minimized.

Copy link

rpratesh commented Feb 27, 2018

This worked for me on Ubuntu 16.04
Before deleting all the containers, force stop them:
docker ps -q -a | xargs docker stop

Then delete the containers using:
docker ps -q -a | xargs docker rm

Now delete all the dangling images using:
docker rmi $(docker images | grep “^<none>” | awk ‘{print $3}’)

@stiv-yakovenko

This comment has been minimized.

Copy link

stiv-yakovenko commented Jun 18, 2018

'docker system prune' doesn't remove images on windows

@Barathchander

This comment has been minimized.

Copy link

Barathchander commented Aug 31, 2018

You can try out below script, i am using that below

#!/bin/bash

timestamp=$(date +%Y%m%d_%H%M%S)
log_path="`pwd`"
filename=docker_cleanup_$timestamp.log
log=$log_path/$filename


docker_space_before(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space:" >> $log
echo $CURRENTSPACE >>$log
}
docker_find (){
echo "#####################################################################" >> $log
echo "Finding images" >> $log
echo "#####################################################################" >> $log
REMOVEIMAGES=`docker images | grep " [days|months|weeks]* ago" | awk '{print $3}'`

echo "Listing images that needs to be cleaned up" >> $log
echo $REMOVEIMAGES >>$log

}

docker_cleanup(){
echo "#####################################################################" >> $log
echo "Cleaning images" >> $log
echo "#####################################################################" >> $log
docker rmi ${REMOVEIMAGES}
}

docker_space_after(){
CURRENTSPACE=`docker system df`
echo "Current Docker Space, after clean up:" >> $log
echo $CURRENTSPACE >>$log
}
docker_space_before
docker_find
docker_cleanup
docker_space_after
@jfsdcgy

This comment has been minimized.

Copy link

jfsdcgy commented Sep 12, 2018

'docker system prune' doesn't remove images on windows

same here, have you found any solution?

@MagicJohnJang

This comment has been minimized.

Copy link

MagicJohnJang commented Sep 20, 2018

For Windows, what's more:

Stop and remove by image name =>

FOR /f "tokens=*" %i IN ('docker ps -a --filter "ancestor=ImageNameHere" -q') DO docker stop %i && docker rm %i

...

(don't forget to change %i to %%i in batch file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.