Can not remove images even though no container is running

MacosDockerDocker ContainerDocker Image

Macos Problem Overview


I had multiple stopped containers and images in my machine.
I wanted to clean up and removed all containers:
docker ps -a returns nothing.
I run docker rmi $(docker images -q) to remove the cached images but I get:

> Error response from daemon: conflict: unable to delete ... > (must be forced) - image is referenced in multiple repositories

What repositories is it talking about?

Macos Solutions


Solution 1 - Macos

You cannot remove images having multiple repositories without the force modifier, see Docker docs for more info.

docker images
REPOSITORY                   TAG      IMAGE ID            CREATED           SIZE
repository/image-name        tag      a8e6fa672e89        10 days ago         344MB
repository2/image-name       tag      a8e6fa672e89        10 days ago         344MB

If you want to do it manually, instead of using the image id to remove the images, you must remove the repository/tag that you don't need using image names:

docker rmi a8e6fa672e89
Error response from daemon: conflict: unable to delete a8e6fa672e89 (must be forced) - image is referenced in multiple repositories

Remove the repository/tag you don't need:

docker rmi repository/image-name:tag
Untagged: repository/image-name:tag
Untagged: repository/image-name:tag@sha256:64b5a02e2bb3ee4d4b7c0982e8e2e5eb68bdfd0fb096fce22b6c030dafb53a33

(Repeat last step until only one repository/tag remains) And now you will be able to remove the image:

docker rmi a8e6fa672e89
Untagged: repository2/image-name:tag
Deleted: sha256:a8e6fa672e89b399bd3ac52b96c031e6816a69191d1fd7e6a1839fd643e3c751
Deleted: sha256:9861dd7b5783217515f571fdcfa6729e1e38af3ae9c971026e5a317b12fc5905

If you use the -f flag and specify the image’s short or long ID, then rmi untags and removes all images that match the specified ID.

Solution 2 - Macos

The "repositories" it is talking about is part of the first column of a docker images:

docker images
REPOSITORY                   TAG      IMAGE ID            CREATED           SIZE
repository/image-name        tag      a8e6fa672e89        10 days ago         344MB
repository2/image-name       tag      a8e6fa672e89        10 days ago         344MB

(I take the samples which Gabriel showed in his answer)

Here we have two repositories: "repository" and "repository2". As you also can see, both images have the same IMAGE ID.

A docker images -q lists all available IMAGE ID's. Thus if you want to remove an IMAGE ID which is referenced by two images you get the error that you have mentioned.

Solution: You can remove the image by its name instead of its ID:

docker rmi repository/image-name:tag

Solution 3 - Macos

To remove a Docker image forcefully, which is referring to multiple repositories, just use the command:

sudo docker rmi -f image_id

Solution 4 - Macos

You can clean up all containers. First of all, you need to stop all containers with: docker stop $(docker ps -aq). Finally, remove all containers with: docker rm $(docker ps -aq). You can do it all in one command docker rm $(docker stop $(docker ps -aq)).

If you want to remove all containers data:

docker container prune
docker network prune
docker system prune
docker volume prune
docker builder prune

Solution 5 - Macos

The underlying problem is that you are trying to remove the image, but the image is tagged as Tarun Banda wrote. So don't delete the image by its id, but by its tag. That will untag the image, and then delete it.

Here an example to cleanup old containers:

docker images | grep '3 weeks ago' | awk '{print $1 ":" $2}' | xargs -n 1 docker rmi

Solution 6 - Macos

If you are sure you want to remove all your images, you can use this command:

docker images | awk '{print $1":"$2}' | egrep -E 'REPOSITORY|TAG' | xargs -n1 docker rmi

Solution 7 - Macos

to delete a single record : sudo docker rmi -f

to delete all images:
sudo docker rmi -f $(sudo docker images -a -q)

The "-f" parameter is important

Solution 8 - Macos

Error response from daemon: conflict: unable to delete 3472c3b5350b (must be forced) - image is referenced in multiple repositories Error response from daemon: conflict: unable to delete 3472c3b5350b (must be forced) - image is referenced in multiple repositories

If this error is coming first untag the image and then it can be deleted. This can be done by using thee following command. docker rmi :

Solution 9 - Macos

 docker rmi `docker images --format="{{.Repository}}:{{.Tag}}"`

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimView Question on Stackoverflow
Solution 1 - MacosGabrielView Answer on Stackoverflow
Solution 2 - MacosHeriView Answer on Stackoverflow
Solution 3 - Macoshussains8View Answer on Stackoverflow
Solution 4 - MacosToirView Answer on Stackoverflow
Solution 5 - MacosBerend de BoerView Answer on Stackoverflow
Solution 6 - MacoskiwoView Answer on Stackoverflow
Solution 7 - MacosSoner PALANCIView Answer on Stackoverflow
Solution 8 - MacosTarun BandaView Answer on Stackoverflow
Solution 9 - MacosAmmar LakisView Answer on Stackoverflow