diff --git a/datacube/index/memory/_products.py b/datacube/index/memory/_products.py index 3dfc2b8fe..52a232296 100644 --- a/datacube/index/memory/_products.py +++ b/datacube/index/memory/_products.py @@ -116,10 +116,12 @@ def update(self, product: Product, return cast(Product, self.get_by_name(product.name)) def delete(self, product: Product): - ids: Iterable[UUID] = self._index.datasets.search_returning(archived=None, product=product.name) - self._index.datasets.purge(ids) + datasets = self._index.datasets.search_returning(('id',), archived=None, product=product.name) + if datasets: + self._index.datasets.purge([ds.id for ds in datasets]) # type: ignore[attr-defined] - del self.by_id[product.id] + if product.id is not None: + del self.by_id[product.id] del self.by_name[product.name] def get_unsafe(self, id_: int) -> Product: diff --git a/datacube/index/postgis/_products.py b/datacube/index/postgis/_products.py index 5aff42b6b..253a3240f 100644 --- a/datacube/index/postgis/_products.py +++ b/datacube/index/postgis/_products.py @@ -242,7 +242,7 @@ def delete(self, product: Product): """ # First find and delete all related datasets product_datasets = self._index.datasets.search_returning(('id',), archived=None, product=product.name) - self._index.datasets.purge([ds.id for ds in product_datasets]) + self._index.datasets.purge([ds.id for ds in product_datasets]) # type: ignore[attr-defined] # Now we can safely delete the Product with self._db_connection() as conn: diff --git a/datacube/index/postgres/_products.py b/datacube/index/postgres/_products.py index b2966df5c..e4cb8709e 100644 --- a/datacube/index/postgres/_products.py +++ b/datacube/index/postgres/_products.py @@ -234,7 +234,7 @@ def delete(self, product: Product): """ # First find and delete all related datasets product_datasets = self._index.datasets.search_returning(('id',), archived=None, product=product.name) - self._index.datasets.purge([ds.id for ds in product_datasets]) + self._index.datasets.purge([ds.id for ds in product_datasets]) # type: ignore[attr-defined] # Now we can safely delete the Product # Pass along metadata type information as well to update indexes/views diff --git a/datacube/scripts/product.py b/datacube/scripts/product.py index 27d58905b..296408e1e 100644 --- a/datacube/scripts/product.py +++ b/datacube/scripts/product.py @@ -161,7 +161,8 @@ def delete_products(index: Index, force: bool, dry_run: bool, product_names: Lis for name in product_names: active_ds = list(index.datasets.search_returning(('id',), archived=False, product=name)) if len(active_ds): - click.echo(f"Product {name} has active datasets: {' '.join([str(ds.id) for ds in active_ds])}") + click.echo(f"Product {name} has active datasets: " + f"{' '.join([str(ds.id) for ds in active_ds])}") # type: ignore[attr-defined] active_product = True if active_product: