From 09a66bd3d514bca7f793793974936f7bcc5121b3 Mon Sep 17 00:00:00 2001 From: Daniel Bedeleanu Date: Wed, 22 Apr 2026 08:54:22 +0300 Subject: [PATCH] feat: clean up image files when item is deleted from catalog - Delete photo_path and photo_thumbnail_path files on item deletion - Handle file not found gracefully with logging - Preserves audit logs while removing actual image files --- backend/routers/items.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/backend/routers/items.py b/backend/routers/items.py index 5a1a29a8..913fdf42 100644 --- a/backend/routers/items.py +++ b/backend/routers/items.py @@ -276,6 +276,25 @@ def delete_item( # [CLEANUP] Delete related InterventionItems to prevent foreign key issues db.query(models.InterventionItem).filter(models.InterventionItem.item_id == item_id).delete() + # [CLEANUP] Delete associated image files + if db_item.photo_path: + try: + photo_file = Path(db_item.photo_path.lstrip("/")) + if photo_file.exists(): + photo_file.unlink() + log.info(f"Deleted photo file: {db_item.photo_path}") + except Exception as e: + log.warning(f"Failed to delete photo file {db_item.photo_path}: {e}") + + if db_item.photo_thumbnail_path: + try: + thumb_file = Path(db_item.photo_thumbnail_path.lstrip("/")) + if thumb_file.exists(): + thumb_file.unlink() + log.info(f"Deleted thumbnail file: {db_item.photo_thumbnail_path}") + except Exception as e: + log.warning(f"Failed to delete thumbnail file {db_item.photo_thumbnail_path}: {e}") + # Audit Logs in database are NOT deleted here to preserve history of actions db.delete(db_item) db.commit()