Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Go: only unlink file-manager files when deleting documents from a knowledge base (#17175)

euvre8408f9a4e233
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