diff --git a/lib/galaxy/managers/hdas.py b/lib/galaxy/managers/hdas.py index b0470b0813e3..13042e76254c 100644 --- a/lib/galaxy/managers/hdas.py +++ b/lib/galaxy/managers/hdas.py @@ -406,17 +406,16 @@ def cleanup_items(self, user: model.User, item_ids: Set[int]) -> StorageItemsCle total_free_bytes = 0 errors: List[StorageItemCleanupError] = [] - for hda_id in item_ids: - try: - hda = self.hda_manager.get_owned(hda_id, user) - self.hda_manager.purge(hda) - success_item_count += 1 - total_free_bytes += int(hda.get_size()) - except BaseException as e: - errors.append(StorageItemCleanupError(item_id=hda_id, error=str(e))) - - if success_item_count: - self.hda_manager.session().flush() + with self.hda_manager.session().begin(): + for hda_id in item_ids: + try: + hda = self.hda_manager.get_owned(hda_id, user) + self.hda_manager.purge(hda) + success_item_count += 1 + total_free_bytes += int(hda.get_size()) + except BaseException as e: + errors.append(StorageItemCleanupError(item_id=hda_id, error=str(e))) + return StorageItemsCleanupResult( total_item_count=len(item_ids), success_item_count=success_item_count, diff --git a/lib/galaxy/managers/histories.py b/lib/galaxy/managers/histories.py index 9aa5868720a6..98be1ed04880 100644 --- a/lib/galaxy/managers/histories.py +++ b/lib/galaxy/managers/histories.py @@ -415,17 +415,16 @@ def cleanup_items(self, user: model.User, item_ids: Set[int]) -> StorageItemsCle total_free_bytes = 0 errors: List[StorageItemCleanupError] = [] - for history_id in item_ids: - try: - history = self.history_manager.get_owned(history_id, user) - self.history_manager.purge(history, flush=False) - success_item_count += 1 - total_free_bytes += int(history.disk_size) - except BaseException as e: - errors.append(StorageItemCleanupError(item_id=history_id, error=str(e))) - - if success_item_count: - self.history_manager.session().flush() + with self.history_manager.session().begin(): + for history_id in item_ids: + try: + history = self.history_manager.get_owned(history_id, user) + self.history_manager.purge(history, flush=False) + success_item_count += 1 + total_free_bytes += int(history.disk_size) + except BaseException as e: + errors.append(StorageItemCleanupError(item_id=history_id, error=str(e))) + return StorageItemsCleanupResult( total_item_count=len(item_ids), success_item_count=success_item_count,