6

I am unable to delete the docker images. Getting below error.

docker image rm -f $(docker image ls -aq)
Error response from daemon: conflict: unable to delete 6ab53ec1a8c9 (cannot be forced) - image is being used by running container d65f1c6b7982
Error response from daemon: conflict: unable to delete 2602b4852593 (cannot be forced) - image has dependent child images

Docker version:

docker --version
Docker version 17.05.0-ce, build 89658be
PAA
  • 1
  • 46
  • 174
  • 282

2 Answers2

8

The problem

Error response from daemon: conflict: unable to delete 6ab53ec1a8c9 (cannot be forced) - image is being used by running container d65f1c6b798

As the message says you have a container running that uses the image your are trying to delete, thus the error.

You can stop the container and afterwards run the command again, but a better way exists...

A Better Way

docker image rm -f $(docker image ls -aq)

Instead of using the above hack from old days you can use now:

docker image prune -a

The flag -a will remove all unused docker images, meaning that the ones being used by running containers will not be touched, thus this may be what you want to use in order to achieve what your are trying to do.

If you want to only remove dangling images without removing the ones you already have built then run the same command without the -a flag:

docker image prune

The help for it:

docker image prune --help                                                                                                                                                                                 
Usage:  docker image prune [OPTIONS]

Remove unused images

Options:
  -a, --all             Remove all unused images, not just dangling ones
      --filter filter   Provide filter values (e.g. 'until=<timestamp>')
  -f, --force           Do not prompt for confirmation
Exadra37
  • 11,244
  • 3
  • 43
  • 57
  • I even used `docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q)` to stop and remove even all containers. That did not help getting rid of the error message when removing the image afterwards in my case, I had to use the `docker image prune -a` command which deleted all of my images - since after stopping and removing all containers, I had forgotten to re-run the containers (which I should have done to keep their images when using that prune command). – questionto42 Feb 12 '21 at 20:28
  • I do not think that this is a good practice. As it is mentioned in the previous comment, you might end up deleting all of your images just because you forgot to run them before pruning .. After all, what happens if you already have different images from irrelevant projects, and not only child-images that preserve dependencies among them? – cestpasmoi Dec 19 '22 at 10:07
  • 1
    Just run instead the command without the `-a` flag. – Exadra37 Dec 19 '22 at 22:38
0

As suggested in this answer you may want to remove all child images, one by one, by running

docker rmi <repo:tag>

However, as suggested in the linked answer, in some cases you may not want to remove the image by specifying the image id that has multiple tags as these may be used by other images.

cestpasmoi
  • 505
  • 6
  • 12
  • 1
    You will prefer to just run `docker image prune` that will remove all dangling images or child images if you prefer to call them that way. Note that `-a` flag is not used, therefore preserving all your current images. – Exadra37 Dec 19 '22 at 22:43
  • Ok now things start to make sense - Thanks a lot! – cestpasmoi Dec 20 '22 at 07:36
  • If you have the message "must be forced" (and not "cannot be forced"), this command is the only one helping you here (though this was *not* the question). I ran `docker rmi 123451344 234143244 123413424` and so on, in a long list through all of the blocking images, and only then some docker setup was ready for a restart from scratch. See [Docker error cannot delete docker container, conflict: unable to remove repository reference](https://stackoverflow.com/a/40283776/11154841). – questionto42 Jan 03 '23 at 16:44