@@ -306,31 +306,30 @@ async def delete_catalog(
306306 db_catalog = await self .client .database .find_catalog (catalog_id )
307307 catalog = self .client .catalog_serializer .db_to_stac (db_catalog , request )
308308
309- # If cascade is true, delete all collections linked to this catalog
310- if cascade :
311- # Extract collection IDs from catalog links
312- collection_ids = []
313- if hasattr (catalog , "links" ) and catalog .links :
314- for link in catalog .links :
315- rel = (
316- link .get ("rel" )
309+ # Extract collection IDs from catalog links
310+ collection_ids = []
311+ if hasattr (catalog , "links" ) and catalog .links :
312+ for link in catalog .links :
313+ rel = (
314+ link .get ("rel" )
315+ if hasattr (link , "get" )
316+ else getattr (link , "rel" , None )
317+ )
318+ if rel == "child" :
319+ href = (
320+ link .get ("href" , "" )
317321 if hasattr (link , "get" )
318- else getattr (link , "rel " , None )
322+ else getattr (link , "href " , "" )
319323 )
320- if rel == "child" :
321- href = (
322- link .get ("href" , "" )
323- if hasattr (link , "get" )
324- else getattr (link , "href" , "" )
325- )
326- if href and "/collections/" in href :
327- # Extract collection ID from href
328- collection_id = href .split ("/collections/" )[- 1 ].split (
329- "/"
330- )[0 ]
331- if collection_id :
332- collection_ids .append (collection_id )
324+ if href and "/collections/" in href :
325+ # Extract collection ID from href
326+ collection_id = href .split ("/collections/" )[- 1 ].split ("/" )[
327+ 0
328+ ]
329+ if collection_id :
330+ collection_ids .append (collection_id )
333331
332+ if cascade :
334333 # Delete each collection
335334 for coll_id in collection_ids :
336335 try :
@@ -348,6 +347,63 @@ async def delete_catalog(
348347 logger .warning (
349348 f"Failed to delete collection { coll_id } : { e } "
350349 )
350+ else :
351+ # Remove catalog link from each collection (orphan them)
352+ for coll_id in collection_ids :
353+ try :
354+ collection = await self .client .get_collection (
355+ coll_id , request = request
356+ )
357+ # Remove the catalog link from the collection
358+ if hasattr (collection , "links" ):
359+ collection .links = [
360+ link
361+ for link in collection .links
362+ if not (
363+ getattr (link , "rel" , None ) == "catalog"
364+ and catalog_id in getattr (link , "href" , "" )
365+ )
366+ ]
367+ elif isinstance (collection , dict ):
368+ collection ["links" ] = [
369+ link
370+ for link in collection .get ("links" , [])
371+ if not (
372+ link .get ("rel" ) == "catalog"
373+ and catalog_id in link .get ("href" , "" )
374+ )
375+ ]
376+
377+ # Update the collection in the database
378+ collection_dict = (
379+ collection .model_dump (mode = "json" )
380+ if hasattr (collection , "model_dump" )
381+ else collection
382+ )
383+ collection_db = (
384+ self .client .database .collection_serializer .stac_to_db (
385+ collection_dict , request
386+ )
387+ )
388+ await self .client .database .client .index (
389+ index = COLLECTIONS_INDEX ,
390+ id = coll_id ,
391+ body = collection_db .model_dump ()
392+ if hasattr (collection_db , "model_dump" )
393+ else collection_db ,
394+ refresh = True ,
395+ )
396+ logger .info (f"Removed catalog link from collection { coll_id } " )
397+ except Exception as e :
398+ error_msg = str (e )
399+ if "not found" in error_msg .lower ():
400+ logger .debug (
401+ f"Collection { coll_id } not found, skipping (may have been deleted elsewhere)"
402+ )
403+ else :
404+ logger .warning (
405+ f"Failed to remove catalog link from collection { coll_id } : { e } "
406+ )
351407
352408 # Delete the catalog
353409 await self .client .database .delete_catalog (catalog_id )
0 commit comments