From 496936be15b9e4f3452c13585db30584d811cd74 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Fri, 20 May 2022 20:11:20 +0300
Subject: [PATCH] Fix for removing images that have depended images.

Can't remove an image that is the base for another image. Just ignore
the error and keep the image.
---
 deepclean/__main__.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/deepclean/__main__.py b/deepclean/__main__.py
index 1e37166..ddaf37b 100644
--- a/deepclean/__main__.py
+++ b/deepclean/__main__.py
@@ -74,9 +74,9 @@ def normalize_names(images_by_name):
     return copy_of_images
 
 
-def deepclean(
+def deepclean(  # noqa: MC0001
     includes=None, excludes=None, verbose=False, dry_run=False
-):  # noqa: MC0001
+):
     """Clean old versions of Docker images."""
     client = docker.from_env()
     images = {i.id: i for i in client.images.list()}
@@ -125,7 +125,13 @@ def deepclean(
             print(f"Would have removed image {Id}.")
         else:
             print(f"Removing image {Id}.")
-            client.images.remove(Id)
+            try:
+                client.images.remove(Id)
+            except docker.errors.APIError as exc:
+                if exc.status_code == 409:
+                    print(f"Image {Id} has descendant images.")
+                else:
+                    raise exc
 
 
 def main():
-- 
GitLab