@@ -499,6 +499,18 @@ async def __get_link(
499499
500500 return link
501501
502+ def find_enclosing (self , file_id : str , kwnown_file_ids : list [str ]) -> str | None :
503+ current_path = Path (file_id )
504+ kwnon_file_paths = [Path (file_id ) for file_id in kwnown_file_ids ]
505+
506+ while current_path and current_path != Path (current_path ).parent :
507+ if current_path in kwnon_file_paths :
508+ return f"{ current_path } "
509+
510+ current_path = Path (current_path ).parent
511+
512+ return None
513+
502514 async def delete_file (
503515 self ,
504516 user_id : UserID ,
@@ -523,22 +535,33 @@ async def delete_file(
523535 if not can .delete :
524536 raise FileAccessRightError (access_right = "delete" , file_id = file_id )
525537
526- with suppress (FileMetaDataNotFoundError ):
527- # NOTE: deleting might be slow, so better ensure we release the connection
528- async with self .engine .acquire () as conn :
529- file : FileMetaDataAtDB = await db_file_meta_data .get (
530- conn , TypeAdapter (SimcoreS3FileID ).validate_python (file_id )
531- )
538+ # NOTE: deleting might be slow, so better ensure we release the connection
539+ async with self .engine .acquire () as conn :
540+ enclosing_file_id = self .find_enclosing (
541+ file_id ,
542+ [
543+ known_file .file_id
544+ for known_file in await db_file_meta_data .list_fmds (
545+ conn , user_id = user_id
546+ )
547+ ],
548+ )
549+
550+ enclosing_file = await db_file_meta_data .get (
551+ conn , TypeAdapter (SimcoreS3FileID ).validate_python (enclosing_file_id )
552+ )
553+
554+ if await db_file_meta_data .exists (conn , file_id ):
555+ await db_file_meta_data .delete (conn , [file_id ])
556+
532557 await get_s3_client (self .app ).delete_objects_recursively (
533- bucket = file .bucket_name ,
558+ bucket = enclosing_file .bucket_name ,
534559 prefix = (
535- ensure_ends_with (file .file_id , "/" )
536- if file .is_directory
537- else file . file_id
560+ ensure_ends_with (enclosing_file .file_id , "/" )
561+ if enclosing_file .is_directory and enclosing_file_id == file_id
562+ else file_id
538563 ),
539564 )
540- async with self .engine .acquire () as conn :
541- await db_file_meta_data .delete (conn , [file .file_id ])
542565
543566 async def delete_project_simcore_s3 (
544567 self , user_id : UserID , project_id : ProjectID , node_id : NodeID | None = None
0 commit comments