279

I am trying

docker rmi c565603bc87f

Error:

Error response from daemon: conflict: unable to delete c565603bc87f (cannot be forced) - image has dependent child images

So i can't delete image even with -f flag. How to delete image then and all of its children ?

Linux and docker version:

uname -a Linux goracio-pc 4.4.0-24-generic #43-Ubuntu SMP Wed Jun 8 19:27:37 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

docker version Client: Version: 1.11.2 API version: 1.23 Go version: go1.5.4 Git commit: b9f10c9 Built: Wed Jun 1 22:00:43 2016 OS/Arch: linux/amd64

Server: Version: 1.11.2 API version: 1.23 Go version: go1.5.4 Git commit: b9f10c9 Built: Wed Jun 1 22:00:43 2016 OS/Arch: linux/amd64

Android Control
  • 496
  • 1
  • 5
  • 14
YakovlevRoman
  • 3,362
  • 3
  • 19
  • 19

25 Answers25

207

In some cases (like in my case) you may be trying to delete an image by specifying the image id that has multiple tags that you don't realize exist, some of which may be used by other images. In which case, you may not want to remove the image.

If you have a case of redundant tags as described here, instead of docker rmi <image_id> use docker rmi <repo:tag> on the redundant tag you wish to remove.

bbarker
  • 11,636
  • 9
  • 38
  • 62
  • 29
    `docker rmi ` worked for me.Your solution is quite simple among those answers, thank you. – Shihe Zhang Jun 13 '18 at 07:45
  • this is should be the corret way to do so for my case to remote older golang images – temple Oct 29 '19 at 01:19
  • This was the ticket for me, thanks. `Untagged: drud/ddev-webserver:20200301_leymannx_apache-junk-built` – rfay Mar 29 '20 at 16:02
  • If anyone has installed Microsoft's [eShopOnContainers](https://github.com/dotnet-architecture/eShopOnContainers) samples you absolutely have to remove each one this way by `repo:tag` because it creates eight tagged images sharing only two image id's. Even Visual Studio won't delete them in its container management window... – mdisibio May 13 '20 at 18:42
  • 3
    The command `docker rmi ` only _untags_, it does not necessarily delete an image. If there is more than one tag referencing this image, or if there is another problem like e.g. the one stated by the OP, the image will still be there. You can check that image still exists with the command `docker images ls --all`. – twan163 Jun 16 '20 at 13:45
  • @twan163 I think you meant `docker image ls --all ` (not `imageS`), right? – Redoman Dec 08 '21 at 10:16
182

You should try to remove unnecessary images before removing the image:

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

After that, run:

docker rmi c565603bc87f
Harvinder
  • 274
  • 3
  • 15
Nguyen Sy Thanh Son
  • 5,300
  • 1
  • 23
  • 33
  • 23
    There are no any dangling images ... docker images -f dangling=true --> Nothing – YakovlevRoman Jun 30 '16 at 14:09
  • 69
    This answer is answering a different question "How do I remove dangling images?" The OP's question is "How do I remove dependent images?" – tudor -Reinstate Monica- Dec 09 '16 at 04:10
  • 11
    To remove dangling images, just use `prune` – Deqing May 18 '17 at 07:26
  • 30
    that command no longer works: `> "docker rmi" requires at least 1 argument.` – samayo Sep 11 '18 at 18:37
  • 3
    @samayo if you get that error, try changing the filter part to: `--filter=dangling=true`. If you still get that error, that just means you don't have any dangling images, and so that in-set command evaluates to an empty string. – HammerN'Songs Oct 08 '18 at 21:29
  • 1
    `unknown flag: --filter` `unknown shorthand flag: 'q' in -q` `unknown flag: --no-trunc)` – Sandburg Oct 07 '19 at 06:58
  • 2
    Answer is not relevant to the question asked. Its not about dangling images but dependent image – Ganesh S Jan 16 '20 at 08:17
  • This was great, solved my problem. Slight improvement: `docker rmi -f ` to force; keeps people from being confused by that issue. – rfay Jan 24 '20 at 14:24
  • 1
    just simply use: `docker rmi -f ` for example: `docker rmi ubuntu:latest -f` will remove image name 'ubuntu' with tag name 'latest' and `-f` is for forcefully removal. it worked for me – Manoj Kumar Feb 10 '21 at 18:04
  • You're wrong, @ManojKumar: `docker rmi --force 1d6c081f3e56`: Error response from daemon: conflict: unable to delete 1d6c081f3e56 (cannot be forced) - image has dependent child images – Theodore R. Smith Nov 22 '21 at 12:18
  • This command only succeeds when there is at least one image. Don't program like that. Use instead: docker images --filter "dangling=true" -q --no-trunc |xargs -r docker rmi – Raúl Salinas-Monteagudo Jun 21 '23 at 21:18
92

all previous answers are correct but here is one solution which is just deleteing all of your images forcefully (use this command at your own risk it will delete all of your images)

docker rmi $(docker images -q) -f

enter image description here

grepit
  • 21,260
  • 6
  • 105
  • 81
  • 3
    This worked for me; I just wanted to blow away all my local images. Note, I had to update to work with my docker version (18.09.7): `docker image rm $(docker image ls -a -q) -f` – akagixxer Jan 03 '20 at 22:14
  • All of your images that dont have a running container... this is quite useful really. – Derek Adair Oct 28 '21 at 21:16
75

find the image id and parent id for all image created after the image in question with the following:

docker inspect --format='{{.Id}} {{.Parent}}' $(docker images --filter since=<image_id> -q)

Then you call command:

docker rmi {sub_image_id} 

"sub_image_id" is ID of dependent image

Nguyen Vu Quang
  • 831
  • 6
  • 5
  • So good to delete intermediate images of a specific image. Thanks!! – A.Villegas Mar 14 '19 at 11:36
  • 1
    unknown flag --filter :/ – SamuraiJack Mar 29 '20 at 16:40
  • 5
    This seems to be the actual solution to the question! – Paolo Apr 13 '20 at 17:19
  • 4
    Here's how you can do this in one line: ```docker inspect --format='{{.Id}} {{.Parent}}' $(docker images --filter since=65f9b4839725 -q) | cut -d' ' -f1 | cut -d: -f2 | xargs docker rmi``` – Matt Friedman Mar 24 '21 at 01:00
  • This is the correct solution – wasserholz Jan 18 '22 at 09:51
  • Here's an useful command to debug image dependencies: `docker inspect --format='{{.Size}} {{truncate .Id 16}} {{if .Parent}}-> {{truncate .Parent 16}}{{else}} ----------------{{end}} {{.RepoTags}}' $(docker images --all -q) | sort -k2 | numfmt --field=1 --to=iec-i --pad=6 --suffix=B` – Joja May 18 '23 at 14:08
  • @MattFriedman Thanks, but just want to point out you might need to add `--force` to the end of that if you get the error `image is referenced in multiple repositories`. – Scott G Aug 03 '23 at 16:26
35

What worked to me was to use the REPOSITORY:TAG combination rather than IMAGE ID.

When I tried to delete a docker image with the command docker rmi <IMAGE ID> with no containers associated with this image I had the message:

$ docker rmi 3f66bec2c6bf
Error response from daemon: conflict: unable to delete 3f66bec2c6bf (cannot be forced) - image has dependent child images

I could delete with success when I used the command docker rmi RPOSITORY:TAG

$ docker rmi ubuntu:18.04v1
Untagged: ubuntu:18.04v1
Edu Costa
  • 1,414
  • 13
  • 13
  • 1
    Indeed. Any explanation why this weird behaviour? – RodrigoM Jan 11 '19 at 16:41
  • 1
    This worked for me as well. In my case I had an out of date Ubuntu image, it was not referenced in any other image as a parent, but still it was unable to remove. `docker rmi 93fd78260bd1` failed, but then `docker tag 93fd78260bd1 ubuntu:temp && docker rmi ubuntu:temp` was successful. – Thomas Lobker Feb 19 '19 at 12:32
  • work for me, was also updating an out of date image. anyone know why it fails w id? – strider Jun 04 '19 at 17:59
  • 4
    This doesn't really delete the image. It just removes a duplicate tag for that image (hence the message `Untagged: ubuntu:18.04v1`). If you do `docker images -a`, you will probably see `3f66bec2c6bf` still listed. If the image were truly deleted, you would get the message `Deleted: 3f66bec2c6bf` – wisbucky Oct 23 '19 at 23:15
  • also does not seem to work if there is no tag, as when mistakenly committing a container to an image with the same name and tag as the original (oops!!) – guitarpicva Aug 27 '23 at 17:51
27

THIS COMMAND REMOVES ALL IMAGES (USE WITH CAUTION)

Have you tried to use --force

sudo docker rmi $(sudo docker images -aq) --force

This above code run like a charm even doe i had the same issue

Ic3fr0g
  • 1,199
  • 15
  • 26
Dapter20
  • 353
  • 4
  • 5
14

If you want to Untag Docker Images

docker rmi <rep:tag>

If you want to Removing Docker Images

docker image rm <image_id>

Ex: Type docker image ls to show info of Images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              3.6            60f85556d5d2        4 days ago          174MB

docker rmi python:3.6

docker image rm 60f85556d5d2

Beginer
  • 365
  • 4
  • 12
11

The answer here is to find all descendent children, which has an answer here:

docker how can I get the list of dependent child images?

Then use that to remove the child images in order.

9

Here's a script to remove an image and all the images that depend on it.

#!/bin/bash

if [[ $# -lt 1 ]]; then
    echo must supply image to remove;
    exit 1;
fi;

get_image_children ()
{
    ret=()
    for i in $(docker image ls -a --no-trunc -q); do
        #>&2 echo processing image "$i";
        #>&2 echo parent is $(docker image inspect --format '{{.Parent}}' "$i")
        if [[ "$(docker image inspect --format '{{.Parent}}' "$i")" == "$1" ]]; then
            ret+=("$i");
        fi;
    done;
    echo "${ret[@]}";
}

realid=$(docker image inspect --format '{{.Id}}' "$1")
if [[ -z "$realid" ]]; then
    echo "$1 is not a valid image.";
    exit 2;
fi;
images_to_remove=("$realid");
images_to_process=("$realid");
while [[ "${#images_to_process[@]}" -gt 0 ]]; do
    children_to_process=();
    for i in "${!images_to_process[@]}"; do
        children=$(get_image_children "${images_to_process[$i]}");
        if [[ ! -z "$children" ]]; then
            # allow word splitting on the children.
            children_to_process+=($children);
        fi;
    done;
    if [[ "${#children_to_process[@]}" -gt 0 ]]; then
        images_to_process=("${children_to_process[@]}");
        images_to_remove+=("${children_to_process[@]}");
    else
        #no images have any children. We're done creating the graph.
        break;
    fi;
done;
echo images_to_remove = "$(printf %s\n "${images_to_remove[@]}")";
indices=(${!images_to_remove[@]});
for ((i="${#indices[@]}" - 1; i >= 0; --i)) ; do
    image_to_remove="${images_to_remove[indices[i]]}"
    if [[ "${image_to_remove:0:7}" == "sha256:" ]]; then
        image_to_remove="${image_to_remove:7}";
    fi
    echo removing image "$image_to_remove";
    docker rmi "$image_to_remove";
done
lmat - Reinstate Monica
  • 7,289
  • 6
  • 48
  • 62
9

Trying to delete image id : b721d1cdaac7

docker rmi b721d1cdaac7 -f

Response : Error response from daemon: conflict: unable to delete b721d1cdaac7 (cannot be forced) - image has dependent child images

To delete all child images command :

  docker image rm $(docker images --filter since=b721d1cdaac7 -q) -f

It will first untagged and removed all child images

Sneha Mule
  • 641
  • 8
  • 6
7
# docker rm $(docker ps -aq)

After that use the command as Nguyen suggested.

camino
  • 10,085
  • 20
  • 64
  • 115
  • This doesn't work. This command removes all the containers. But the image may still have dependent child images: for example, if the image being deleted is a intermediate image of a Docker build. – GolDDranks Oct 05 '20 at 05:37
5

Building on Simon Brady's brute force method here, if you don't have a ton of images you can use this shell function:

recursive_remove_image() {
  for image in $(docker images --quiet --filter "since=${1}")
  do
    if [ $(docker history --quiet ${image} | grep ${1}) ]
    then
      recursive_remove_image "${image}"
    fi
  done
  echo "Removing: ${1}"
  docker rmi -f ${1}
}

and then call it using recursive_remove_image <image-id>.

slushy
  • 3,277
  • 1
  • 18
  • 24
5

Please run this docker command

  1. docker image rm -f $(docker image ls --filter dangling=true -q)

and then run

  1. docker image rm -f $(docker image ls -a -q)

I found the above commands very helpful after working for many hours.

  1. Otherwise, you can run prune script. https://gist.github.com/sethbergman/cb0f1f700b1f6474b9738191055c9fb7
Aamir M Meman
  • 1,645
  • 14
  • 15
3

When i want to remove some unused image with name "<none>" in docker i face with the problem unable to delete a354bbc7c9b7 (cannot be forced) - image has dependent child images.So for solving this problem:

sudo docker ps -a

CONTAINER ID        IMAGE                       COMMAND                  CREATED             STATUS                         PORTS                                              NAMES
01ee1276bbe0        lizard:1                    "/bin/sh -c 'java ..."   About an hour ago   Exited (1) About an hour ago                                                      objective_lewin
49d73d8fb023        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Up 19 hours                    0.0.0.0:8091->8091/tcp                             pedantic_bell
405fd452c788        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Created                                                                           infallible_varahamihira
532257a8b705        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Created                                                                           demo-default
9807158b3fd5        javaapp:latest              "/usr/bin/java -ja..."   19 hours ago        Created                                                                           xenodochial_kilby
474930241afa        jenkins                     "/bin/tini -- /usr..."   13 days ago         Up 4 days                      0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   myjenkins
563d8c34682f        mysql/mysql-server:latest   "/entrypoint.sh my..."   3 weeks ago         Up 4 days (healthy)            0.0.0.0:3306->3306/tcp, 33060/tcp                  mymysql
b4ca73d45d20        phpmyadmin/phpmyadmin       "/run.sh phpmyadmin"     4 weeks ago         Exited (0) 3 weeks ago                                                            phpmyadmin

you can see that i have several Images with name javaapp:latest and different container name. So, i killed and remove all container of "javaapp:latest" container with:

sudo docker stop "containerName"

sudo docker rm "containrName"

Then

sudo docker rmi -f "imageId"

So i can remove all the images with name "<none>"

goodluck

Community
  • 1
  • 1
Saman Salehi
  • 1,004
  • 1
  • 12
  • 19
3

As explained here, I used the following way to identify dependent images and delete them,

image_id=123456789012

docker images -a -q --filter since=$image_id |
xargs docker inspect --format='{{.Id}} {{.Parent}}'

You would see something similar to this as the output:

sha256:f7ef19862215ec0bf7a6b103504d213e1c001691703808f4154689cfbb5f14f9 sha256:a7d2efad2847bd10e5223980ed80f5781c716eddbf6131a3cf97614e7f2db97f
sha256:03690ae141346203959d0ae1b3e8d34b7a4232095d774af57dda6282fce99cc4 sha256:5713074659bb5352496ea680a903eba2f66e0495538c9db37336f4ba92994ea8
sha256:311f587811942d328edc52e5953d794eb9b81fe392512080d9fc1d350a6b2024 sha256:aa674f7f2621946db257720c378377b8714739d20879542d875b84c53b59bc75

Then you can delete those images one by one as below:

docker image rm f7ef19862215ec0bf7a6b103504d213e1c001691703808f4154689cfbb5f14f9

Output something similar to bellow:

Untagged: prathap/cognitive_robotics_gpu:v1
Untagged: prathap/cognitive_robotics_gpu@sha256:db6e7543a13e9a96241c985b9b3145b8fd65effb68c183301385b495875f1a5a
Deleted: sha256:03690ae141346203959d0ae1b3e8d34b7a4232095d774af57dda6282fce99cc4
Deleted: sha256:263f655670436758f8e3f23f31170083fc8d60c4eebe01a5b3fda1e73bed3ad1
GPrathap
  • 7,336
  • 7
  • 65
  • 83
2

Expanding on the answer provided by @Nguyen - this function can be added to your .bashrc etc and then called from the commandline to help clean up any image has dependent child images errors...

You can run the function as yourself, and if a docker ps fails, then it will run the docker command with sudo and prompt you for your password.

Does NOT delete images for any running containers!

docker_rmi_dependants ()                                                                                                                                                         
{ 
  DOCKER=docker
  [ docker ps >/dev/null 2>&1 ] || DOCKER="sudo docker"

  echo "Docker: ${DOCKER}"

  for n in $(${DOCKER} images | awk '$2 == "<none>" {print $3}');
  do  
    echo "ImageID: $n";
    ${DOCKER} inspect --format='{{.Id}} {{.Parent}}' $(${DOCKER} images --filter since=$n -q);
  done;

  ${DOCKER} rmi $(${DOCKER} images | awk '$2 == "<none>" {print $3}')
}

I also have this in my .bashrc file...

docker_rm_dangling ()  
{ 
  DOCKER=docker
  [ docker ps >/dev/null 2>&1 ] || DOCKER="sudo docker"

  echo "Docker: ${DOCKER}"

  ${DOCKER} images -f dangling=true 2>&1 > /dev/null && YES=$?;                                                                                                                  
  if [ $YES -eq 1 ]; then
    read -t 30 -p "Press ENTER to remove, or CTRL-C to quit.";
    ${DOCKER} rmi $(${DOCKER} images -f dangling=true -q);
  else
    echo "Nothing to do... all groovy!";
  fi  
}

Works with:

$ docker --version 
Docker version 17.05.0-ce, build 89658be
Android Control
  • 496
  • 1
  • 5
  • 14
1

I also got this issue, I could resolve issue with below commands. this may be cause, the image's container is running or exit so before remove image you need to remove container

docker ps -a -f status=exited : this command shows all the exited containers so then copy container Id and then run below commands to remove container

docker rm #containerId : this command remove container this may be issue that mention "image has dependent child images"

Then try to remove image with below command

docker rmi #ImageId

Damith Asanka
  • 934
  • 10
  • 12
1

I had this issue and none of the short answers here worked, even in the page mentioned by @tudor above. I thought I would share here how I got rid of the images. I came up with the idea that dependent images must be >= the size of the parent image, which helps identify it so we can remove it.

I listed the images in size order to see if I could spot any correlations:

docker images --format '{{.Size}}\t{{.Repository}}\t{{.Tag}}\t{{.ID}}' | sort -h -r | column -t

What this does, is use some special formatting from docker to position the image size column first, then run a human readable sort in reverse order. Then I restore the easy-to-read columns.

Then I looked at the <none> containers, and matched the first one in the list with a similar size. I performed a simple docker rmi <image:tag> on that image and all the <none> child images went with it.

The problem image with all the child images was actually the damn myrepo/getstarted-lab image I used when I first started playing with docker. It was because I had created a new image from the first test image which created the chain.

Hopefully that helps someone else at some point.

Chris Gillatt
  • 1,026
  • 8
  • 13
1

Trying to delete image id : b721d1cdaac7

 docker rmi b721d1cdaac7 -f

Response : Error response from daemon: conflict: unable to delete b721d1cdaac7 (cannot be forced) - image has dependent child images

To delete all child images

docker image rm $(docker images --filter since=b721d1cdaac7 -q) -f
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Sneha Mule
  • 641
  • 8
  • 6
  • Proceed with CAUTION when running the 2nd command, it doesn't delete all child (or dependent) images, it deletes ALL the images that were created since the parent image. – Prasannjeet Singh May 03 '23 at 12:55
0

Force deleting a list of images (exclude version 10, for example)

docker images | grep version | grep -v version10 > images.txt && for img in $( awk -F" " '{print $3}' /root/images.txt ) ; do docker rmi -f $img; done

Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
0

Suppose we have a Dockerfile

FROM ubuntu:trusty
CMD ping localhost

We build image from that without TAG or naming

docker build .

Now we have a success report "Successfully built 57ca5ce94d04" If we see the docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              57ca5ce94d04        18 seconds ago      188MB
ubuntu              trusty              8789038981bc        11 days ago         188MB

We need to first remove the docker rmi 57ca5ce94d04

Followed by

docker rmi 8789038981bc

By that image will be removed!

A forced removal of all as suggested by someone

docker rmi $(docker images -q) -f
vimal krishna
  • 2,886
  • 28
  • 22
0

Just simply use:

docker rmi <image:tag> -f

for example:

docker rmi ubuntu:latest -f 

will remove image name ubuntu with tag name latest and -f is for forcefully removal.

it worked for me

Manoj Kumar
  • 5,273
  • 1
  • 26
  • 33
0

Assure you haven't hit the rate-limit error if behind a corporate proxy.

If, by chance, you're behind a corporate proxy and using Windows, you may want to try this simple fix. I found several responses here helpful. However, even after running a docker image prune and other commands, I uncovered the lovely error (and root cause), which was previously masked: "toomanyrequests: You have reached your pull rate limit..."

Fix.

  1. Open a Windows Terminal/DOS prompt and enter:
  • ipconfig/release
  • [...]
  • ipconfig/renew

This might just save you a call/ticket with IT. ;)

lino
  • 51
  • 2
  • 5
-1

Image Layer: Repositories are often referred to as images or container images, but actually they are made up of one or more layers. Image layers in a repository are connected together in a parent-child relationship. Each image layer represents changes between itself and the parent layer.

The docker building pattern uses inheritance. It means the version i depends on version i-1. So, we must delete the version i+1 to be able to delete version i. This is a simple dependency.

If you wanna delete all images except the last one (the most updated) and the first (base) then we can export the last (the most updated one) using docker save command as below.

docker save -o <output_file> <your_image-id> | gzip <output_file>.tgz

Then, now, delete all the images using image-id as below.

docker rm -f <image-id i> | docker rm -f <image i-1> | docker rm -f <image-id i-2> ... <docker rm -f <image-id i-k> # where i-k = 1

Now, load your saved tgz image as below.

gzip -c <output_file.tgz> | docker load

see the image-id of your loaded image using docker ps -q. It doesn't have tag and name. You can simply update tag and name as done below.

docker tag <image_id> group_name/name:tag
Uddhav P. Gautam
  • 7,362
  • 3
  • 47
  • 64
-2

you can just do this:

➜ ~ sudo docker rmi 4ed13257bb55 -f Deleted: sha256:4ed13257bb5512b975b316ef482592482ca54018a7728ea1fc387e873a68c358 Deleted: sha256:4a478ca02e8d2336595dcbed9c4ce034cd15f01229733e7d93a83fbb3a9026d3 Deleted: sha256:96df41d1ce6065cf75d05873fb1f9ea9fed0ca86addcfcec7722200ed3484c69 Deleted: sha256:d95efe864c7096c38757b80fddad12819fffd68ac3cc73333ebffaa42385fded

unnamed-bull
  • 361
  • 4
  • 15