Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to remove unused Docker containers and images
  1. Delete all containers

     $ docker ps -q -a | xargs docker rm
    

-q prints only the container IDs -a prints all containers

Notice that it uses xargs to issue a remove container command for each container ID

  1. Delete all untagged images

     $ docker rmi $(docker images | grep “^<none>” | awk ‘{print $3}’)
    

awk must use a single quote (this filters all image IDs)

@lukewendling

This comment has been minimized.

Show comment Hide comment
@lukewendling

lukewendling Dec 18, 2015

rm non-ascii chars

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

rm non-ascii chars

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

@quyen91

This comment has been minimized.

Show comment Hide comment
@quyen91

quyen91 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

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.

Show comment Hide comment
@siripuramrk

siripuramrk Jan 16, 2016

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

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

@siripuramrk

This comment has been minimized.

Show comment Hide comment
@siripuramrk

siripuramrk Jan 16, 2016

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

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

@manolohk

This comment has been minimized.

Show comment Hide comment
@manolohk

manolohk 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")

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.

Show comment Hide comment
@jakthom

jakthom 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

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.

Show comment Hide comment
@jarek-przygodzki

jarek-przygodzki 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

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.

Show comment Hide comment
@rorono

rorono May 6, 2016

+1

rorono commented May 6, 2016

+1

@Desperado

This comment has been minimized.

Show comment Hide comment
@Desperado

Desperado May 30, 2016

jarek-przygodzki works for me :)

jarek-przygodzki works for me :)

@HWiese1980

This comment has been minimized.

Show comment Hide comment
@HWiese1980

HWiese1980 Jun 5, 2016

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

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

@jarek-przygodzki

This comment has been minimized.

Show comment Hide comment
@jarek-przygodzki

jarek-przygodzki 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.

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.

Show comment Hide comment
@sangram-chavan

sangram-chavan Jun 22, 2016

!/bin/bash

Delete all containers

docker rm $(docker ps -a -q)

Delete all images

docker rmi $(docker images -q)

!/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.

Show comment Hide comment
@vishalvsh1

vishalvsh1 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

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.

Show comment Hide comment
@luchsamapparat

luchsamapparat Sep 27, 2016

@HWiese1980

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

@HWiese1980

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

@Azerothian

This comment has been minimized.

Show comment Hide comment
@Azerothian

Azerothian 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

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.

Show comment Hide comment
@ianmalott

ianmalott 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: docker/docker#26108

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: docker/docker#26108

@siddharthbarman

This comment has been minimized.

Show comment Hide comment
@siddharthbarman

siddharthbarman 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 } }

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.

Show comment Hide comment
@elutsky

elutsky Jan 4, 2017

Worked great on the Windows container! Thanks!

elutsky commented Jan 4, 2017

Worked great on the Windows container! Thanks!

@dermeister0

This comment has been minimized.

Show comment Hide comment
@dermeister0

dermeister0 Jan 12, 2017

PowerShell

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

PowerShell

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

This comment has been minimized.

Show comment Hide comment
@alexlopes

alexlopes Mar 8, 2017

Great gist! Exactly what I was looking for

Great gist! Exactly what I was looking for

@rolf-d2i

This comment has been minimized.

Show comment Hide comment
@rolf-d2i

rolf-d2i 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"

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.

Show comment Hide comment
@joao-parana

joao-parana Apr 24, 2017

@ianmalott list plus:

docker network prune

@ianmalott list plus:

docker network prune
@buutuud

This comment has been minimized.

Show comment Hide comment
@buutuud

buutuud May 10, 2017

mark

buutuud commented May 10, 2017

mark

@kalakodumuri

This comment has been minimized.

Show comment Hide comment
@kalakodumuri

kalakodumuri 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.

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.

Show comment Hide comment
@GerBreOwn

GerBreOwn 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.

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.

Show comment Hide comment
@GerBreOwn

GerBreOwn 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!!!

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.

Show comment Hide comment
@GerBreOwn

GerBreOwn 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.

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.

Show comment Hide comment
@88plug

88plug Jun 21, 2017

@GerBreOwn

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

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.

Show comment Hide comment
@jarrekk

jarrekk Jul 6, 2017

docker image prune

jarrekk commented Jul 6, 2017

docker image prune

@ronyrun

This comment has been minimized.

Show comment Hide comment
@ronyrun

ronyrun Jul 11, 2017

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

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.

Show comment Hide comment
@rutgerhofste

rutgerhofste 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.

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.

Show comment Hide comment
@pascalandy

pascalandy 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

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.

Show comment Hide comment
@gnanasundar

gnanasundar 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")

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.

Show comment Hide comment
@joao-parana

joao-parana 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.

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.

@KevinMaschke

This comment has been minimized.

Show comment Hide comment
@KevinMaschke

KevinMaschke 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

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.

Show comment Hide comment
@friscochap

friscochap Dec 26, 2017

Remove all docker images marked as "none" in Powershell

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

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

@pratesh

This comment has been minimized.

Show comment Hide comment
@pratesh

pratesh 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}’)

pratesh 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}’)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment