From cdf38c4a7205336cf3a2f1b701f09a68c8ae900a Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:13:17 +0100 Subject: [PATCH] fix: deleting a folder or all chats leaves their replies and sub-agents running (#35058) Deleting a single chat stops a reply that is still being written and any sub-agent it handed work to. Deleting a folder with its chats, or all chats, removed the chats but left that work running, so replies kept streaming from the provider and a sub-agent left running kept its background slot, refusing the next delegation with "Async subagent capacity reached". Both now stop the running work of every chat they delete, including its sub-agents, as deleting a single chat does. Fixes #35056 --- backend/open_webui/models/chats.py | 12 ++++++++++++ backend/open_webui/routers/chats.py | 3 +++ backend/open_webui/routers/folders.py | 8 +++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 8368b68b07..474369eb02 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -757,6 +757,18 @@ class ChatTable: ) return list(result.scalars().all()) + async def get_chat_ids_by_user_id(self, user_id: str, db: AsyncSession | None = None) -> list[str]: + async with get_async_db_context(db) as session: + result = await session.execute(select(Chat.id).filter_by(user_id=user_id)) + return list(result.scalars().all()) + + async def get_chat_ids_by_user_id_and_folder_id( + self, user_id: str, folder_id: str, db: AsyncSession | None = None + ) -> list[str]: + async with get_async_db_context(db) as session: + result = await session.execute(select(Chat.id).filter_by(user_id=user_id, folder_id=folder_id)) + return list(result.scalars().all()) + async def get_children_by_parent_id(self, parent_chat_id: str, user_id: str) -> list: async with get_async_db_context() as session: result = await session.execute( diff --git a/backend/open_webui/routers/chats.py b/backend/open_webui/routers/chats.py index ed5ddd3ec4..6a651ba120 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -838,6 +838,9 @@ async def delete_all_user_chats( detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) + for chat_id in await Chats.get_chat_ids_by_user_id(user.id, db=db): + await stop_item_tasks(request.app.state.redis, chat_id) + result = await Chats.delete_chats_by_user_id(user.id, db=db) if result: await publish_event( diff --git a/backend/open_webui/routers/folders.py b/backend/open_webui/routers/folders.py index 353c512a88..369befc29c 100644 --- a/backend/open_webui/routers/folders.py +++ b/backend/open_webui/routers/folders.py @@ -34,7 +34,7 @@ from open_webui.models.shared_chats import ChatShareMode from open_webui.models.users import Users from open_webui.routers.notes import check_notes_access, truncate_note_data from open_webui.socket.main import close_note -from open_webui.tasks import has_active_tasks +from open_webui.tasks import has_active_tasks, stop_item_tasks from open_webui.utils.access_control import ( has_permission, validate_access_grants, @@ -1115,6 +1115,12 @@ async def delete_folder_by_id( for folder_id in folder_ids: if delete_contents: + for chat_id in await Chats.get_chat_ids_by_user_id_and_folder_id( + folder_owner_id, folder_id, db=db + ): + await stop_item_tasks(request.app.state.redis, chat_id) + for child_id in await Chats.get_internal_chat_ids_by_parent_id(chat_id, folder_owner_id): + await stop_item_tasks(request.app.state.redis, child_id) await Chats.delete_chats_by_user_id_and_folder_id(folder_owner_id, folder_id, db=db) for note in await Notes.get_notes_by_folder_id_and_user_id(folder_id, folder_owner_id, db=db): await Notes.delete_note_by_id(note.id, db=db)