From 8408f9a4e233abf22243bbeae8f59349def00490 Mon Sep 17 00:00:00 2001 From: euvre <93761161+euvre@users.noreply.github.com> Date: Wed, 22 Jul 2026 12:14:22 +0800 Subject: [PATCH] Go: only unlink file-manager files when deleting documents from a knowledge base (#17175) --- internal/service/document/document_crud.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/service/document/document_crud.go b/internal/service/document/document_crud.go index d482d6324c..7c310009e9 100644 --- a/internal/service/document/document_crud.go +++ b/internal/service/document/document_crud.go @@ -392,7 +392,9 @@ func (s *DocumentService) rollbackAddFileFromKBError(doc *entity.Document, kbID // cleanupFileReferences deletes file2document mappings for docID, and for each // referenced file, only hard-deletes the file record and its storage blob when -// no other document still references the same file_id. +// the file is a knowledgebase-owned upload (source_type == knowledgebase) and +// no other document still references the same file_id. Files linked from file +// management are only unlinked — the file record and blob stay intact. func (s *DocumentService) cleanupFileReferences(docID string) { mappings, mapErr := s.file2DocumentDAO.GetByDocumentID(docID) if mapErr != nil { @@ -418,7 +420,8 @@ func (s *DocumentService) cleanupFileReferences(docID string) { common.Logger.Warn(fmt.Sprintf("cleanupFileReferences: failed to delete f2d for %s: %v", docID, delErr)) } - // For each file, only delete the record and blob when no other doc references it + // For each file, only delete the record and blob when it is a + // knowledgebase-owned upload and no other doc references it for _, fileID := range fileIDs { remaining, remErr := s.file2DocumentDAO.GetByFileID(fileID) if remErr != nil { @@ -435,6 +438,9 @@ func (s *DocumentService) cleanupFileReferences(docID string) { common.Logger.Warn(fmt.Sprintf("cleanupFileReferences: file not found %s: %v", fileID, fErr)) continue } + if entity.FileSource(file.SourceType) != entity.FileSourceKnowledgebase { + continue // linked from file management — unlink only, keep the file + } if _, delErr := fileDAO.DeleteByIDs([]string{fileID}); delErr != nil { common.Logger.Warn(fmt.Sprintf("cleanupFileReferences: failed to delete file %s: %v", fileID, delErr)) continue // keep the blob so the live file row still has its object