Skip to content

fix(kb): clean up documents and media when deleting a knowledge base#9303

Open
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/kb-delete-orphan-cleanup
Open

fix(kb): clean up documents and media when deleting a knowledge base#9303
he-yufeng wants to merge 1 commit into
AstrBotDevs:masterfrom
he-yufeng:fix/kb-delete-orphan-cleanup

Conversation

@he-yufeng

@he-yufeng he-yufeng commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

KnowledgeBaseManager.delete_kb() deletes the KnowledgeBase row and the vector DB, but the KB's KBDocument and KBMedia rows carry kb_id with 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_id deletes KBMedia explicitly because a bulk DELETE does 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-deletes KBMedia, KBDocument, and the KnowledgeBase row for the KB, mirroring delete_document_by_id.

  • delete_kb() calls it (after delete_vec_db()) instead of only session.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 / 运行截图或测试结果

$ .venv/bin/python -m pytest tests/unit/test_kb_document_cleanup.py -q
6 passed

Red-before-green check: with the two child-table deletes removed from delete_kb_by_id, test_delete_kb_cleans_documents_and_media fails because the KBDocument/KBMedia rows survive the KB deletion; with the fix in place they are gone.

Checklist / 检查清单

  • 👀 Well-tested (unit tests + red-before-green verification).
  • 🤓 No new dependencies.
  • 😮 No malicious code.

Summary by Sourcery

Ensure deleting a knowledge base also removes its associated documents and media, avoiding orphaned rows in SQLite.

Bug Fixes:

  • Fix knowledge base deletion leaving orphaned KBDocument and KBMedia records in the SQLite database.
  • Ensure deletion of one knowledge base does not affect documents and media belonging to other knowledge bases.

Enhancements:

  • Add a KBSQLiteDatabase.delete_kb_by_id helper to encapsulate deletion of a knowledge base and its related documents and media.
  • Update KnowledgeBaseManager.delete_kb to use the new database helper for consistent cleanup behavior.

Tests:

  • Extend kb document cleanup unit tests to cover whole-knowledge-base deletion and isolation of data across multiple knowledge bases.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 17, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant