fix(kb): clean up documents and media when deleting a knowledge base#9303
fix(kb): clean up documents and media when deleting a knowledge base#9303he-yufeng wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a mechanism to clean up associated documents and multimedia records when a knowledge base is deleted, preventing orphan records in the database. Specifically, it adds a delete_kb_by_id method to kb_db_sqlite.py to delete KBMedia, KBDocument, and KnowledgeBase records in a single transaction, updates kb_mgr.py to use this new method, and adds corresponding unit tests to verify the cleanup behavior. There are no review comments, so I have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
KnowledgeBaseManager.delete_kb()deletes theKnowledgeBaserow and the vector DB, but the KB'sKBDocumentandKBMediarows carrykb_idwith no ORM relationship or foreign-key cascade. Deleting a knowledge base therefore leaves all of its documents and media orphaned in the SQLite database.This is the same cascade gap #9120 fixed for single-document deletion (
delete_document_by_iddeletesKBMediaexplicitly because a bulkDELETEdoes not trigger ORM cascade) — the whole-KB delete path was the missed sibling.删除知识库时只删了
KnowledgeBase行和向量库,但该库的KBDocument/KBMedia通过kb_id关联、没有 ORM/外键级联,会把全部文档和多媒体记录留成孤儿(与 #9120 修的单文档删除是同一类问题)。Modifications / 改动点
Add
KBSQLiteDatabase.delete_kb_by_id(kb_id)that bulk-deletesKBMedia,KBDocument, and theKnowledgeBaserow for the KB, mirroringdelete_document_by_id.delete_kb()calls it (afterdelete_vec_db()) instead of onlysession.delete(kb_helper.kb).Unit tests in
tests/unit/test_kb_document_cleanup.py: whole-KB deletion leaves no orphaned document/media, and deleting one KB does not touch another KB's rows.This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Red-before-green check: with the two child-table deletes removed from
delete_kb_by_id,test_delete_kb_cleans_documents_and_mediafails because theKBDocument/KBMediarows survive the KB deletion; with the fix in place they are gone.Checklist / 检查清单
Summary by Sourcery
Ensure deleting a knowledge base also removes its associated documents and media, avoiding orphaned rows in SQLite.
Bug Fixes:
Enhancements:
Tests: