Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove dangling docker images
docker rmi $(docker images -q -f dangling=true)
@programster

This comment has been minimized.

Copy link

@programster programster commented Jun 17, 2015

The "full" command would be written as such (easier to understand)

docker rmi $(docker images --quiet --filter "dangling=true")
@apatrushev

This comment has been minimized.

Copy link

@apatrushev apatrushev commented Oct 12, 2015

You will have the error message in case of no dangling images. The version without the error:

docker images -qf dangling=true | xargs docker rmi
@focusaurus

This comment has been minimized.

Copy link

@focusaurus focusaurus commented Nov 21, 2015

xargs will need --no-run-if-empty (-r) to bypass executing docker rmi with no arguments. So:

docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi

@hannes-ucsc

This comment has been minimized.

Copy link

@hannes-ucsc hannes-ucsc commented Sep 13, 2016

--no-run-if-empty

Not on OS X, though.

@tkachoff

This comment has been minimized.

Copy link

@tkachoff tkachoff commented Sep 26, 2016

Some images can't be removed without -f option

docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi -f

@ph4un00b

This comment has been minimized.

Copy link

@ph4un00b ph4un00b commented Oct 2, 2016

$ docker images --quiet --filter=dangling=true | xargs docker rmi -f (on OSX, dunno if dangerous -f cmd)

@psrodka-neurosys

This comment has been minimized.

Copy link

@psrodka-neurosys psrodka-neurosys commented Jan 12, 2017

docker rmi $(docker images -q -f dangling=true)
Doesn't work in Windows cmd

@zuver

This comment has been minimized.

Copy link

@zuver zuver commented Jan 17, 2017

This works for Windows cmd:
FOR /f "tokens=*" %i IN ('docker images -q -f "dangling=true"') DO docker rmi %i

If you have PowerShell you can use this:
docker rmi $(docker images -q -f dangling=true)

@romanesko

This comment has been minimized.

Copy link

@romanesko romanesko commented Feb 10, 2017

Using this for any linux/osx system:

if docker images -f "dangling=true" | grep ago --quiet; then
    docker rmi -f $(docker images -f "dangling=true" -q)
fi
@MBlagui

This comment has been minimized.

Copy link

@MBlagui MBlagui commented May 26, 2017

You do that easier:

docker image prune

@jaymecd

This comment has been minimized.

Copy link

@jaymecd jaymecd commented Jun 20, 2017

@MBlagui, bravo!

@kennypatierno

This comment has been minimized.

Copy link

@kennypatierno kennypatierno commented Sep 27, 2017

@MBlagui you're my hero for this

docker container prune works well too, thanks!

@martindariocernadas

This comment has been minimized.

Copy link

@martindariocernadas martindariocernadas commented Nov 16, 2017

docker container prune ; docker images prune

@simonm3

This comment has been minimized.

Copy link

@simonm3 simonm3 commented Dec 3, 2017

The docs say docker prune "removes all dangling images". And it even issues a warning "this will remove all dangling images". So why does it not remove mine?

ubuntu@ip-172-31-26-48:~$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0 B
ubuntu@ip-172-31-26-48:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
simonm3/registrm    latest              bd3ef6d73785        7 minutes ago       468 MB
simonm3/registr     latest              7dd15943671b        2 hours ago         2.51 GB
simonm3/registrm    <none>              f8f22986e85a        45 hours ago        2.93 GB
simonm3/registrm    <none>              c1b9d8606371        45 hours ago        2.93 GB
simonm3/registrm    <none>              f1ca97467106        45 hours ago        2.93 GB
simonm3/registr     <none>              6a5af88f93a7        45 hours ago        4.42 GB
simonm3/registrm    <none>              93dc81b0fa7a        45 hours ago        2.93 GB
simonm3/registrm    <none>              dbc50510d499        45 hours ago        2.93 GB
simonm3/registr     <none>              0101ca5d4a25        45 hours ago        4.42 GB
simonm3/registrm    <none>              d8f85df39f0a        46 hours ago        2.93 GB
simonm3/registrm    <none>              053892e9798b        46 hours ago        2.93 GB
simonm3/registrm    <none>              3845056cf241        46 hours ago        2.92 GB
simonm3/registr     <none>              349ef3c589bd        46 hours ago        4.42 GB
simonm3/registrm    <none>              5dcbb0db4710        2 days ago          2.92 GB
simonm3/registr     <none>              e403f74861f6        2 days ago          4.32 GB
simonm3/registr     <none>              c12ca8819c24        5 days ago          4.32 GB
simonm3/registrm    <none>              4bb8ed8154b7        6 days ago          2.92 GB
ubuntu@ip-172-31-26-48:~$ docker images -q -f dangling=true
f8f22986e85a
c1b9d8606371
f1ca97467106
6a5af88f93a7
93dc81b0fa7a
dbc50510d499
0101ca5d4a25
d8f85df39f0a
053892e9798b
3845056cf241
349ef3c589bd
5dcbb0db4710
e403f74861f6
c12ca8819c24
4bb8ed8154b7
@r-chris

This comment has been minimized.

Copy link

@r-chris r-chris commented Dec 4, 2017

@simonm3 you probably want to run docker container prune - otherwise docker still thinks they are being run by a stopped container

@VladiPeled

This comment has been minimized.

Copy link

@VladiPeled VladiPeled commented Jun 3, 2018

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

@liudonghua123

This comment has been minimized.

Copy link

@liudonghua123 liudonghua123 commented Sep 7, 2018

nice jobs

@JinnaBalu

This comment has been minimized.

Copy link

@JinnaBalu JinnaBalu commented Mar 6, 2019

Some images can't be removed without -f option

docker images --quiet --filter=dangling=true | xargs --no-run-if-empty docker rmi -f

This works perfect, does the job

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