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)

rm non-ascii chars

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

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
$ docker-machine restart default
$ docker images -q | xargs docker rmi

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

This worked for me :) " $ docker rmi $(docker images -q -f 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 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 commented May 5, 2016 edited

I use this script


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


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


# 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

k4zzk commented May 6, 2016


jarek-przygodzki works for me :)

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

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


Delete all containers

docker rm $(docker ps -a -q)

Delete all images

docker rmi $(docker images -q)

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

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


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



docker rm $(docker ps -a -q)


docker rmi $(docker images -q)



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


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

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


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

New Data Management commands PR: docker/docker#26108

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 commented Jan 4, 2017

Worked great on the Windows container! Thanks!


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

Great gist! Exactly what I was looking for

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


import docker
dockDaemon = docker.from_env()

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

for id in imgid:
        print "Trying to remove image",id
        print "Removed image",id
    except docker.errors.APIError,e:
        print "skipping image ",id,"as this is still in use"

@ianmalott list plus:

docker network prune

buutuud commented May 10, 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 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"

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

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???


88plug commented Jun 21, 2017 edited


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

jarrekk commented Jul 6, 2017 edited

docker image prune

ronyrun commented Jul 11, 2017

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

rutgerhofste commented Jul 22, 2017 edited

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.

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