277

I've the following images:

alex@alexvps:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              70c0e19168cf        5 days ago          1.069 GB
<none>              <none>              c2ce80b62174        8 days ago          399.2 MB
<none>              <none>              60afe4036d97        8 days ago          325.1 MB

and when I try to remove one of them I get:

alex@alexvps:~$ sudo docker rmi 60afe4036d97
Error: Conflict, 60afe4036d97 wasn't deleted
2014/01/28 00:54:00 Error: failed to remove one or more images

How can I remove them? Why is there such conflict?

Bryson
  • 1,186
  • 2
  • 13
  • 26
alessmar
  • 4,689
  • 7
  • 43
  • 52

16 Answers16

499

In order to delete all images, use the given command

docker rmi $(docker images -q)

In order to delete all containers, use the given command

docker rm $(docker ps -a -q)

Warning: This will destroy all your images and containers. It will not be possible to restore them!

This solution is provided by Techoverflow.net.

Sunny
  • 407
  • 5
  • 15
  • 17
    Simple and straight. Also you below command to force remove images. 'docker rmi -f $(docker images -q)' – dimuthu Apr 22 '15 at 03:10
  • I got an illegal variable name error doing `docker rmi $(docker images -q)` – David Morales May 17 '16 at 00:28
  • 38
    The windows powershell equivalent is `docker images -q | %{docker rmi -f $_}` – BeatingToADifferentRobot Jun 28 '16 at 21:17
  • 1
    First i used the cmd on @muheed [Answer] (http://stackoverflow.com/a/30475249/2747020) to unlock the removing of some images: `sudo docker ps -a -q | xargs -n 1 -I {} sudo docker rm {}` – Razec Luar Jun 30 '16 at 14:30
  • If container is already running use `docker rm -f $(docker ps -a -q) – Raju Mar 19 '17 at 12:04
  • 1
    Windows Powershell for docker containers `docker ps -a -q | %{docker rm -f $_}` – Dexter Apr 12 '17 at 01:34
  • 2
    As @alexyz78 notes below, you can now use `docker system prune`. So to wipe everyhing: `docker kill $(docker ps -q)` to stop containers followed by a `docker system prune -a` will remove everything - see https://stackoverflow.com/a/44309011/247708 – Bharat Aug 08 '17 at 14:03
  • @DavidMorales my edit fixes the problem you have `docker rmi < echo $(docker images -q | tr "\n" " ")` – Bizmate Jan 25 '18 at 23:58
  • order should be like: sudo docker rm $(sudo docker ps -a -q) sudo docker rmi $(sudo docker images -q) – Girish Gupta May 09 '18 at 06:44
  • What I do is filter by image name first `docker rm $(docker ps -a | grep "" | awk '{print $1}')` – radtek Sep 07 '18 at 17:20
  • Does `docker rm $(docker ps -a -q)` also delete image along with containers? or does it only delete containers? – variable May 05 '20 at 05:39
109

Possible reason: The reason can be that this image is currently used by a running container. In such case, you can list running containers, stop the relevant container and then remove the image:

docker ps
docker stop <containerid>
docker rm <containerid>
docker rmi <imageid>

If you cannnot find container by docker ps, you can use this to list all already exited containers and remove them.

docker ps -a | grep 60afe4036d97
docker rm <containerid>

Note: Be careful of deleting all exited containers at once in case you use Volume-Only containers. These stay in Exit state, but contains useful data.

Jiri
  • 16,425
  • 6
  • 52
  • 68
  • Actually, you would get a message specifying that. Maybe another image is based on this one? Try removing any images that you created from this image first. – qkrijger Jan 28 '14 at 23:37
  • 12
    I was able to remove the image with `sudo docker ps -a | grep Exit | awk '{print $1}' | sudo xargs docker rm` (thanks to https://github.com/dotcloud/docker/issues/3258) and then with `sudo docker rmi 70c0e19168cf` – alessmar Jan 29 '14 at 06:18
  • 2
    Be careful deleting images in `Exit` state. If you are using data volume-only containers they do not stay running. – aceofspades Jul 13 '14 at 17:58
  • 1
    awk can grep: `awk '/Exit/ {print $1}'` – Dwight Spencer Nov 02 '15 at 19:14
  • Your commands throw this `Error response from daemon: No such container: ...`. It's hard to tell what has to be replaced, and what should I replace with. Don't use `...` – Chris Vilches Jul 02 '17 at 00:02
60

The reason for the error is that even though the image did not have any tag, there still exists a container created on that image which might be in the exited state. So you need to ensure that you have stopped and deleted all containers created on those images. The following command helps you in removing all containers that are not running:

docker rm `docker ps -aq --no-trunc --filter "status=exited"`

Now this removes all the dangling non-intermediate <none> images:

docker rmi `docker images --filter 'dangling=true' -q --no-trunc`

Note: To stops all running containers:

docker stop `docker ps -q`
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eldos
  • 3,132
  • 3
  • 29
  • 32
19

In Bash:

for i in `sudo docker images|grep \<none\>|awk '{print $3}'`;do sudo docker rmi $i;done

This will remove all images with name "<none>". I found those images redundant.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
marcell
  • 1,332
  • 9
  • 10
19

The image could be currently used by a running container, so you first have to stop and remove the container(s).

docker stop <container-name>
docker rm <container-id>

Then you could try deleting the image:

docker rmi <image-id>

You must be sure that this image doesn't depend on other images (otherwise you must delete them first).

I had a strange case in which I had no more containers still alive (docker ps -a returned nothing) but I couldn't manage to delete the image and its image-dependency.

To solve these special cases you could force the image removal with this:

docker rmi -f <image-id>
BSMP
  • 4,596
  • 8
  • 33
  • 44
madx
  • 6,723
  • 4
  • 55
  • 59
15

I found the answer in this command:

docker images --no-trunc | grep none | awk '{print $3}' | xargs docker rmi

I had your problem when I deleted some images that were being used, and I didn't realise (using docker ps -a).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ivangoblin
  • 161
  • 1
  • 3
11

Since Docker ver. 1.13.0 (January 2017) there's the system prune command:

$ docker system prune --help

Usage:  docker system prune [OPTIONS]

Remove unused data

Options:
-a, --all     Remove all unused images not just dangling ones
-f, --force   Do not prompt for confirmation
    --help    Print usage
alessmar
  • 4,689
  • 7
  • 43
  • 52
7

Simply you can aadd --force at the end of the command. Like:

sudo docker rmi <docker_image_id> --force

To make it more intelligent you can add as:

First stop all running containers.

sudo docker stop $(docker ps | grep <your_container_name> | awk '{print $1}')

Now remove container

sudo docker rm $(docker ps | grep <your_container_name> | awk '{print $1}')

Now as a last step, remove docker image

sudo docker rmi $(docker images | grep <your_image_name> | awk '{print $3}') --force

Here in docker ps $1 is the first column, i.e. the Docker container ID.

And docker images $3 is the third column, i.e. the Docker image ID.

Note: In above commands sudo is used in case you might / might not have privileges to execute command. if it gives you error, try removing sudo.

7

In addition to Sunny's answer:

In order to delete all images on a Windows machine (with Docker for Windows) in a PowerShell window, do:

docker images -q | %{docker rmi -f $_}

In order to delete all containers on a Windows machine (with Docker for Windows) in a PowerShell window, do:

docker ps -a -q | %{docker rm -f $_}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Vogel
  • 71
  • 1
  • 4
6

First, remove all the containers using the following command

sudo docker ps -a -q | xargs -n 1 -I {} sudo docker rm {}

Then, remove the image by its ID using the following command

sudo docker rmi <image-id>
Sunny
  • 407
  • 5
  • 15
Muheed
  • 69
  • 1
  • 1
5

Use

docker image prune -all

or

docker image prune -a

Remove all dangling images. If -a is specified, it will also remove all images not referenced by any container.

Note: You are prompted for confirmation before the prune removes anything, but you are not shown a list of what will potentially be removed. In addition, docker image ls does not support negative filtering, so it difficult to predict what images will actually be removed.

As stated under Docker's documentation for prune.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dylan
  • 499
  • 1
  • 10
  • 21
4

You have to stop/delete all unnecessary containers created on that images first.

Have a look: How to remove old Docker containers.

After that use @marcell solution.

Community
  • 1
  • 1
tgrigoryan
  • 249
  • 1
  • 2
  • 6
4

Remove all the containers

docker ps -q -a | xargs docker rm

Force remove all the Docker images

docker rmi -f $(docker images -f dangling=true -q)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
3

The most compact version of a command to remove all untagged images is:

docker rmi $(docker images | grep "^<none>" | awk '{print $"3"}')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
red
  • 634
  • 1
  • 6
  • 12
3

To delete some Docker image you must execute the following command:

$ docker rmi <docker_image_id>

So, to delete all Docker images you can execute the following command:

$ docker rmi $(docker images -q)

Now, if you want delete all Docker images (including images that are in use), you can add the flag -f, for example:

$ docker rmi -f $(docker images -q)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

If you want to automatically/periodically clean up exited containers and remove images and volumes that aren't in use by a running container you can download the Docker image meltwater/docker-cleanup.

That way you don't need to go clean it up by hand.

Just run:

docker run -d -v /var/run/docker.sock:/var/run/docker.sock:rw  -v /var/lib/docker:/var/lib/docker:rw --restart=unless-stopped meltwater/docker-cleanup:latest

It will run every 30 minutes (or however long you set it using DELAY_TIME=1800 option) and clean up exited containers and images.

More details: https://github.com/meltwater/docker-cleanup/blob/master/README.md

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Innocent Anigbo
  • 4,435
  • 1
  • 19
  • 19