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()