Skip to content

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

Fix: delete document, does not delete the real file in minio (#20022)

Wang Qi840ae6daf58a
diff --git a/internal/service/document/document_crud.go b/internal/service/document/document_crud.go
index d748199175..73e396bd8e 100644
--- a/internal/service/document/document_crud.go
+++ b/internal/service/document/document_crud.go
@@ -298,7 +298,7 @@ func (s *DocumentService) deleteDocumentFull(ctx context.Context, docID string)
}
fileCleanupCtx := context.WithoutCancel(ctx)
- if err = s.cleanupFileReferences(fileCleanupCtx, docID); err != nil {
+ if err = s.cleanupFileReferences(fileCleanupCtx, docID, doc.KbID); err != nil {
return fmt.Errorf("document deleted but file cleanup failed: %w", err)
}
@@ -487,7 +487,7 @@ func (s *DocumentService) rollbackAddFileFromKBError(ctx context.Context, doc *e
// 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(ctx context.Context, docID string) error {
+func (s *DocumentService) cleanupFileReferences(ctx context.Context, docID, kbID string) error {
mappings, mapErr := s.file2DocumentDAO.GetByDocumentID(ctx, dao.DB, docID)
if mapErr != nil {
common.Warn(fmt.Sprintf("cleanupFileReferences: failed to get f2d mappings for %s: %v", docID, mapErr))
@@ -542,9 +542,10 @@ func (s *DocumentService) cleanupFileReferences(ctx context.Context, docID strin
if file.Location != nil && *file.Location != "" {
storageImpl := storage.GetStorageFactory().GetStorage()
if storageImpl != nil {
- rmErr := removeObjectBestEffort(ctx, storageImpl, file.ParentID, *file.Location)
+ // Dataset uploads use the KB ID; ParentID is the file-manager folder.
+ rmErr := removeObjectBestEffort(ctx, storageImpl, kbID, *file.Location)
if rmErr != nil {
- common.Warn(fmt.Sprintf("cleanupFileReferences: failed to remove blob %s/%s: %v", file.ParentID, *file.Location, rmErr))
+ common.Warn(fmt.Sprintf("cleanupFileReferences: failed to remove blob %s/%s: %v", kbID, *file.Location, rmErr))
}
}
}
diff --git a/internal/service/document/document_test.go b/internal/service/document/document_test.go
index 985718b885..baf0ceddef 100644
--- a/internal/service/document/document_test.go
+++ b/internal/service/document/document_test.go
@@ -830,8 +830,16 @@ func TestDeleteDocumentFull_CleansUpFile2Document(t *testing.T) {
insertTestDoc(t, "doc-1", "kb-1", 10, 5)
insertTestIngestionTask(t, "task-1", "user-1", "doc-1", "kb-1")
loc := "path/to/blob"
- insertTestFile(t, "file-1", "kb-1", "test.pdf", &loc)
+ insertTestFile(t, "file-1", "dataset-folder-1", "test.pdf", &loc)
insertTestFile2Document(t, "f2d-1", "file-1", "doc-1")
+ store := newFakeUploadStorage()
+ if err := store.Put(t.Context(), "kb-1", loc, []byte("document")); err != nil {
+ t.Fatalf("store document blob: %v", err)
+ }
+ factory := storage.GetStorageFactory()
+ originalStorage := factory.GetStorage()
+ factory.SetStorage(store)
+ t.Cleanup(func() { factory.SetStorage(originalStorage) })
svc := testDocumentService(t)
ctx := t.Context()
@@ -853,6 +861,9 @@ func TestDeleteDocumentFull_CleansUpFile2Document(t *testing.T) {
if len(files) != 0 {
t.Fatalf("expected 0 files, got %d", len(files))
}
+ if store.ObjExist(ctx, "kb-1", loc) {
+ t.Fatal("document blob should be deleted")
+ }
}
func TestDeleteDocumentFull_SharedFilePreserved(t *testing.T) {
@@ -1884,7 +1895,7 @@ func TestCleanupFileReferences_NoMappings(t *testing.T) {
svc := testDocumentService(t)
// Should not panic with no f2d mappings
ctx := t.Context()
- svc.cleanupFileReferences(ctx, "no-mappings")
+ svc.cleanupFileReferences(ctx, "no-mappings", "kb-1")
}
func TestCleanupFileReferences_SingleFileDeleted(t *testing.T) {
@@ -1897,7 +1908,7 @@ func TestCleanupFileReferences_SingleFileDeleted(t *testing.T) {
svc := testDocumentService(t)
ctx := t.Context()
- svc.cleanupFileReferences(ctx, "doc-1")
+ svc.cleanupFileReferences(ctx, "doc-1", "kb-1")
// f2d gone
mappings, _ := dao.NewFile2DocumentDAO().GetByDocumentID(ctx, db, "doc-1")
@@ -1922,7 +1933,7 @@ func TestCleanupFileReferences_SharedFileSurvives(t *testing.T) {
svc := testDocumentService(t)
ctx := t.Context()
- svc.cleanupFileReferences(ctx, "doc-1")
+ svc.cleanupFileReferences(ctx, "doc-1", "kb-1")
// f2d for doc-1 gone
mappings, _ := dao.NewFile2DocumentDAO().GetByDocumentID(ctx, db, "doc-1")