Skip to content

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

fix(search): delete the Search App row like Python does, drop unused soft-delete helpers (#21005)

qinling02105fe45905b16d
diff --git a/internal/dao/api_token.go b/internal/dao/api_token.go
index bed736b61b..63b8c7a595 100644
--- a/internal/dao/api_token.go
+++ b/internal/dao/api_token.go
@@ -43,12 +43,6 @@ func (dao *APITokenDAO) GetByTenantID(ctx context.Context, db *gorm.DB, tenantID
return tokens, err
}
-// DeleteByTenantID deletes all API tokens by tenant ID (hard delete)
-func (dao *APITokenDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Where("tenant_id = ?", tenantID).Delete(&entity.APIToken{})
- return result.RowsAffected, result.Error
-}
-
// GetByAPIToken gets user by API token
func (dao *APITokenDAO) GetByAPIToken(ctx context.Context, db *gorm.DB, token string) (*entity.APIToken, error) {
var apiToken entity.APIToken
diff --git a/internal/dao/chat.go b/internal/dao/chat.go
index f4e78f60dc..dc2a7dd2ef 100644
--- a/internal/dao/chat.go
+++ b/internal/dao/chat.go
@@ -176,12 +176,6 @@ func (dao *ChatDAO) UpdateManyByID(ctx context.Context, db *gorm.DB, updates []m
return tx.Commit().Error
}
-// DeleteByTenantID deletes all chats by tenant ID (hard delete)
-func (dao *ChatDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("tenant_id = ?", tenantID).Delete(&entity.Chat{})
- return result.RowsAffected, result.Error
-}
-
// GetAllDialogIDsByTenantID gets all dialog IDs by tenant ID
func (dao *ChatDAO) GetAllDialogIDsByTenantID(ctx context.Context, db *gorm.DB, tenantID string) ([]string, error) {
var dialogIDs []string
diff --git a/internal/dao/document.go b/internal/dao/document.go
index 7578ad47df..bb3f3c8bbc 100644
--- a/internal/dao/document.go
+++ b/internal/dao/document.go
@@ -434,12 +434,6 @@ func (dao *DocumentDAO) GetChunkingConfig(ctx context.Context, db *gorm.DB, docI
return config, nil
}
-// DeleteByTenantID deletes all documents by tenant ID (hard delete)
-func (dao *DocumentDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("tenant_id = ?", tenantID).Delete(&entity.Document{})
- return result.RowsAffected, result.Error
-}
-
// GetAllDocIDsByKBIDs gets all document IDs by knowledge base IDs
func (dao *DocumentDAO) GetAllDocIDsByKBIDs(ctx context.Context, db *gorm.DB, kbIDs []string) ([]map[string]string, error) {
var docs []struct {
diff --git a/internal/dao/file.go b/internal/dao/file.go
index b366dfdb8e..0988e90033 100644
--- a/internal/dao/file.go
+++ b/internal/dao/file.go
@@ -275,12 +275,6 @@ func (dao *FileDAO) UpdateByID(ctx context.Context, db *gorm.DB, id string, upda
return db.WithContext(ctx).Model(&entity.File{}).Where("id = ?", id).Updates(updates).Error
}
-// DeleteByTenantID deletes all files by tenant ID (hard delete)
-func (dao *FileDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("tenant_id = ?", tenantID).Delete(&entity.File{})
- return result.RowsAffected, result.Error
-}
-
// DeleteByIDs deletes files by IDs (hard delete)
func (dao *FileDAO) DeleteByIDs(ctx context.Context, db *gorm.DB, ids []string) (int64, error) {
if len(ids) == 0 {
diff --git a/internal/dao/kb.go b/internal/dao/kb.go
index 8f1d2467cb..7783bde7ec 100644
--- a/internal/dao/kb.go
+++ b/internal/dao/kb.go
@@ -158,11 +158,6 @@ func (dao *KnowledgebaseDAO) UpdateByID(ctx context.Context, db *gorm.DB, id str
return db.WithContext(ctx).Model(&entity.Knowledgebase{}).Where("id = ?", id).Updates(updates).Error
}
-// Delete soft deletes a knowledge base by setting status to invalid
-func (dao *KnowledgebaseDAO) Delete(ctx context.Context, db *gorm.DB, id string) error {
- return db.WithContext(ctx).Model(&entity.Knowledgebase{}).Where("id = ?", id).Update("status", string(entity.StatusInvalid)).Error
-}
-
// GetByID retrieves a knowledge base by ID
func (dao *KnowledgebaseDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.Knowledgebase, error) {
var kb entity.Knowledgebase
@@ -497,12 +492,6 @@ func (dao *KnowledgebaseDAO) GetByNameInUserTenants(ctx context.Context, db *gor
return kbs, err
}
-// DeleteByTenantID deletes all knowledge bases by tenant ID (hard delete)
-func (dao *KnowledgebaseDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("tenant_id = ?", tenantID).Delete(&entity.Knowledgebase{})
- return result.RowsAffected, result.Error
-}
-
// GetKBIDsByTenantIDSimple GetKBIDsByTenantID gets all knowledge base IDs by tenant ID
func (dao *KnowledgebaseDAO) GetKBIDsByTenantIDSimple(ctx context.Context, db *gorm.DB, tenantID string) ([]string, error) {
var kbIDs []string
diff --git a/internal/dao/langfuse.go b/internal/dao/langfuse.go
index a748c8709c..9f61af7205 100644
--- a/internal/dao/langfuse.go
+++ b/internal/dao/langfuse.go
@@ -66,19 +66,6 @@ func (dao *LangfuseDAO) UpdateByTenantID(ctx context.Context, db *gorm.DB, tenan
return nil
}
-// DeleteByTenantID deletes the Langfuse credentials row for a tenant
-// (mirrors delete_model / delete_ty_tenant_id).
-func (dao *LangfuseDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) error {
- res := db.WithContext(ctx).Where("tenant_id = ?", tenantID).Delete(&entity.TenantLangfuse{})
- if res.Error != nil {
- return res.Error
- }
- if res.RowsAffected == 0 {
- return gorm.ErrRecordNotFound
- }
- return nil
-}
-
func (dao *LangfuseDAO) SaveByTenantID(ctx context.Context, db *gorm.DB, row *entity.TenantLangfuse) error {
return db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "tenant_id"}},
diff --git a/internal/dao/langfuse_test.go b/internal/dao/langfuse_test.go
index 7613edcff2..61b87e614b 100644
--- a/internal/dao/langfuse_test.go
+++ b/internal/dao/langfuse_test.go
@@ -56,7 +56,7 @@ func TestLangfuseDAO_GetByTenantID_NotFound(t *testing.T) {
}
}
-func TestLangfuseDAO_CRUD(t *testing.T) {
+func TestLangfuseDAO_CreateGetUpdate(t *testing.T) {
db := setupLangfuseTestDB(t)
pushDB(t, db)
dao := NewLangfuse()
@@ -105,16 +105,4 @@ func TestLangfuseDAO_CRUD(t *testing.T) {
if got.SecretKey != "sk-2" || got.PublicKey != "pk-2" || got.Host != "https://eu.langfuse.com" {
t.Fatalf("update not applied: %+v", got)
}
-
- // 4. DeleteByTenantID
- if err = dao.DeleteByTenantID(ctx, db, "tenant-1"); err != nil {
- t.Fatalf("failed to delete: %v", err)
- }
- got, err = dao.GetByTenantID(ctx, db, "tenant-1")
- if err != nil {
- t.Fatalf("expected nil error after delete, got %v", err)
- }
- if got != nil {
- t.Fatalf("expected row to be deleted, got %+v", got)
- }
}
diff --git a/internal/dao/search.go b/internal/dao/search.go
index 2dd2eb80a1..bbe04abf32 100644
--- a/internal/dao/search.go
+++ b/internal/dao/search.go
@@ -154,9 +154,11 @@ func (dao *SearchDAO) Create(ctx context.Context, db *gorm.DB, search *entity.Se
return db.WithContext(ctx).Create(search).Error
}
-// DeleteByID deletes a search by ID (soft delete by setting status to "0")
+// DeleteByID deletes a search by ID (hard delete), matching Python
+// SearchService.delete_by_id. A soft delete only hid the row from the permission
+// layer and left it in MySQL.
func (dao *SearchDAO) DeleteByID(ctx context.Context, db *gorm.DB, id string) error {
- return db.WithContext(ctx).Model(&entity.Search{}).Where("id = ? AND status = ?", id, "1").Update("status", "0").Error
+ return db.WithContext(ctx).Unscoped().Where("id = ?", id).Delete(&entity.Search{}).Error
}
// UpdateByID updates search by ID
diff --git a/internal/dao/search_test.go b/internal/dao/search_test.go
index b3f0804394..5a2b626072 100644
--- a/internal/dao/search_test.go
+++ b/internal/dao/search_test.go
@@ -278,3 +278,52 @@ func TestSearchDAOListByResourceIDsKeepsOwnerFilter(t *testing.T) {
}
assertSearchIDs(t, rows, []string{"search-t1"}, "owner filter")
}
+
+// TestSearchDAODeleteByIDRemovesRow pins the delete semantics against Python:
+// SearchService.delete_by_id inherits CommonService.delete_by_id, which issues a
+// real DELETE on the `search` table. A soft delete (status = "0") only hid the
+// row from the permission layer and left it in MySQL.
+func TestSearchDAODeleteByIDRemovesRow(t *testing.T) {
+ db := setupSearchDetailDAOTestDB(t)
+ pushDB(t, db)
+ seedOrderableSearches(t, db)
+ ctx := t.Context()
+ d := NewSearchDAO()
+
+ if err := d.DeleteByID(ctx, db, "s-2"); err != nil {
+ t.Fatalf("DeleteByID: %v", err)
+ }
+
+ // The row is gone, not merely marked invalid.
+ assertSearchRowCount(t, db, "s-2", 0)
+ assertSearchRowCount(t, db, "s-1", 1)
+
+ // Deletion is by id alone, so a row the permission layer would already hide
+ // (status != "1") is removed too, exactly as Python deletes it.
+ legacy := entity.Search{
+ ID: "s-legacy",
+ TenantID: "t1",
+ Name: "legacy",
+ CreatedBy: "t1",
+ SearchConfig: entity.JSONMap{},
+ Status: stringPtr("0"),
+ }
+ if err := db.Create(&legacy).Error; err != nil {
+ t.Fatalf("failed to create legacy search: %v", err)
+ }
+ if err := d.DeleteByID(ctx, db, "s-legacy"); err != nil {
+ t.Fatalf("DeleteByID legacy: %v", err)
+ }
+ assertSearchRowCount(t, db, "s-legacy", 0)
+}
+
+func assertSearchRowCount(t *testing.T, db *gorm.DB, id string, want int64) {
+ t.Helper()
+ var count int64
+ if err := db.Model(&entity.Search{}).Where("id = ?", id).Count(&count).Error; err != nil {
+ t.Fatalf("count search %s: %v", id, err)
+ }
+ if count != want {
+ t.Fatalf("search %s row count = %d, want %d", id, count, want)
+ }
+}
diff --git a/internal/dao/skill_search_config.go b/internal/dao/skill_search_config.go
index 3884097b13..c55a1ce42d 100644
--- a/internal/dao/skill_search_config.go
+++ b/internal/dao/skill_search_config.go
@@ -180,8 +180,3 @@ func (dao *SkillSearchConfigDAO) UpdateByTenantAndEmbdID(ctx context.Context, db
result := db.WithContext(ctx).Model(&entity.SkillSearchConfig{}).Where("tenant_id = ? AND space_id = ? AND embd_id = ? AND status = ?", tenantID, normalizeSpaceID(spaceID), embdID, "1").Updates(updates)
return result.Error
}
-
-// Delete deletes a skill search config by ID (soft delete)
-func (dao *SkillSearchConfigDAO) Delete(ctx context.Context, db *gorm.DB, id string) error {
- return db.WithContext(ctx).Model(&entity.SkillSearchConfig{}).Where("id = ?", id).Update("status", "0").Error
-}
diff --git a/internal/dao/task.go b/internal/dao/task.go
index 4a6e8df588..012da0570a 100644
--- a/internal/dao/task.go
+++ b/internal/dao/task.go
@@ -94,12 +94,6 @@ func (dao *TaskDAO) DeleteByDocIDs(ctx context.Context, db *gorm.DB, docIDs []st
return result.RowsAffected, result.Error
}
-// DeleteByTenantID deletes all tasks by tenant ID (hard delete via document join)
-func (dao *TaskDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("doc_id IN (SELECT id FROM document WHERE tenant_id = ?)", tenantID).Delete(&entity.Task{})
- return result.RowsAffected, result.Error
-}
-
// GetByDocID gets all tasks by document ID
func (dao *TaskDAO) GetByDocID(ctx context.Context, db *gorm.DB, docID string) ([]*entity.Task, error) {
var tasks []*entity.Task
diff --git a/internal/dao/tenant.go b/internal/dao/tenant.go
index 39990d8311..3d95ffaefe 100644
--- a/internal/dao/tenant.go
+++ b/internal/dao/tenant.go
@@ -113,11 +113,6 @@ func (dao *TenantDAO) GetAllIDs(ctx context.Context, db *gorm.DB) ([]string, err
return ids, err
}
-// Delete deletes a tenant by ID (soft delete)
-func (dao *TenantDAO) Delete(ctx context.Context, db *gorm.DB, id string) error {
- return db.WithContext(ctx).Model(&entity.Tenant{}).Where("id = ?", id).Update("status", "0").Error
-}
-
// Update updates a tenant by ID
func (dao *TenantDAO) Update(ctx context.Context, db *gorm.DB, id string, updates map[string]interface{}) error {
return db.WithContext(ctx).Model(&entity.Tenant{}).Where("id = ?", id).Updates(updates).Error
diff --git a/internal/dao/tenant_model_provider.go b/internal/dao/tenant_model_provider.go
index 37b5f3ce95..1865ea5658 100644
--- a/internal/dao/tenant_model_provider.go
+++ b/internal/dao/tenant_model_provider.go
@@ -55,12 +55,6 @@ func (dao *TenantModelProviderDAO) GetByTenantIDAndProviderName(ctx context.Cont
return &provider, nil
}
-// DeleteByTenantID deletes all model providers by tenant ID (hard delete)
-func (dao *TenantModelProviderDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("tenant_id = ?", tenantID).Delete(&entity.TenantModelProvider{})
- return result.RowsAffected, result.Error
-}
-
// DeleteByTenantIDAndProviderName DeleteByTenantID deletes all providers by tenant ID (hard delete)
func (dao *TenantModelProviderDAO) DeleteByTenantIDAndProviderName(ctx context.Context, db *gorm.DB, tenantID, providerName string) (int64, error) {
result := db.WithContext(ctx).Unscoped().Where("tenant_id = ? AND provider_name = ?", tenantID, providerName).Delete(&entity.TenantModelProvider{})
diff --git a/internal/dao/tenant_test.go b/internal/dao/tenant_test.go
index 40a6f371d7..ee69b482ef 100644
--- a/internal/dao/tenant_test.go
+++ b/internal/dao/tenant_test.go
@@ -44,39 +44,44 @@ func useTenantDAOTestDB(t *testing.T, db *gorm.DB) {
t.Cleanup(func() { DB = orig })
}
-func TestTenantDAODeleteSoftDeletesTenant(t *testing.T) {
+// TestTenantDAOGetByIDSkipsInactiveTenant pins the behaviour GetByID relies on:
+// it filters status = "1", so a tenant row that is still present but inactive is
+// reported as not found. This is the part of the old soft-delete test that
+// covered live code; it no longer needs a delete helper to set up.
+func TestTenantDAOGetByIDSkipsInactiveTenant(t *testing.T) {
db := setupTenantDAOTestDB(t)
useTenantDAOTestDB(t, db)
ctx := t.Context()
- active := "1"
- tenant := &entity.Tenant{
- ID: "tenant-delete",
- LLMID: "llm",
- EmbdID: "embd",
- ASRID: "asr",
- Img2TxtID: "img2txt",
- RerankID: "rerank",
- ParserIDs: "naive",
- Status: &active,
- }
- if err := NewTenantDAO().Create(ctx, db, tenant); err != nil {
- t.Fatalf("Create() error = %v", err)
- }
-
- if err := NewTenantDAO().Delete(ctx, db, tenant.ID); err != nil {
- t.Fatalf("Delete() error = %v", err)
+ seed := func(id, status string) {
+ t.Helper()
+ tenant := &entity.Tenant{
+ ID: id,
+ LLMID: "llm",
+ EmbdID: "embd",
+ ASRID: "asr",
+ Img2TxtID: "img2txt",
+ RerankID: "rerank",
+ ParserIDs: "naive",
+ Status: &status,
+ }
+ if err := NewTenantDAO().Create(ctx, db, tenant); err != nil {
+ t.Fatalf("Create(%s) error = %v", id, err)
+ }
}
+ seed("tenant-active", "1")
+ seed("tenant-inactive", "0")
- var got entity.Tenant
- if err := db.WithContext(ctx).Where("id = ?", tenant.ID).First(&got).Error; err != nil {
- t.Fatalf("failed to reload tenant: %v", err)
+ got, err := NewTenantDAO().GetByID(ctx, db, "tenant-active")
+ if err != nil {
+ t.Fatalf("GetByID(active) error = %v", err)
}
- if got.Status == nil || *got.Status != "0" {
- t.Fatalf("status = %v, want 0", got.Status)
+ if got.ID != "tenant-active" {
+ t.Fatalf("GetByID(active) = %+v", got)
}
- if _, err := NewTenantDAO().GetByID(ctx, db, tenant.ID); err == nil {
- t.Fatalf("GetByID() after Delete() error = nil, want not found")
+
+ if _, err := NewTenantDAO().GetByID(ctx, db, "tenant-inactive"); err == nil {
+ t.Fatal("GetByID(inactive) error = nil, want not found")
}
}
diff --git a/internal/dao/user.go b/internal/dao/user.go
index 2f1caccae4..cf1916443c 100644
--- a/internal/dao/user.go
+++ b/internal/dao/user.go
@@ -148,11 +148,6 @@ func (dao *UserDAO) Delete(ctx context.Context, db *gorm.DB, id uint) error {
return db.WithContext(ctx).Delete(&entity.User{}, id).Error
}
-// DeleteByID delete user by string ID (soft delete - set status to 0)
-func (dao *UserDAO) DeleteByID(ctx context.Context, db *gorm.DB, id string) error {
- return db.WithContext(ctx).Model(&entity.User{}).Where("id = ?", id).Update("status", "0").Error
-}
-
// HardDelete hard delete user by string ID
func (dao *UserDAO) HardDelete(ctx context.Context, db *gorm.DB, id string) error {
return db.WithContext(ctx).Unscoped().Where("id = ?", id).Delete(&entity.User{}).Error
diff --git a/internal/dao/user_tenant.go b/internal/dao/user_tenant.go
index f60ecaee1f..1e2876a6a7 100644
--- a/internal/dao/user_tenant.go
+++ b/internal/dao/user_tenant.go
@@ -53,11 +53,6 @@ func (dao *UserTenantDAO) Update(ctx context.Context, db *gorm.DB, userTenant *e
return db.WithContext(ctx).Save(userTenant).Error
}
-// Delete delete user tenant relationship (soft delete by setting status to "0")
-func (dao *UserTenantDAO) Delete(ctx context.Context, db *gorm.DB, id string) error {
- return db.WithContext(ctx).Model(&entity.UserTenant{}).Where("id = ?", id).Update("status", "0").Error
-}
-
// GetByUserID gets active user tenant relationships by user ID with context.
func (dao *UserTenantDAO) GetByUserID(ctx context.Context, db *gorm.DB, userID string) ([]*entity.UserTenant, error) {
var relations []*entity.UserTenant
@@ -168,12 +163,6 @@ func (dao *UserTenantDAO) DeleteByUserID(ctx context.Context, db *gorm.DB, userI
return result.RowsAffected, result.Error
}
-// DeleteByTenantID delete user tenant relationships by tenant ID (hard delete)
-func (dao *UserTenantDAO) DeleteByTenantID(ctx context.Context, db *gorm.DB, tenantID string) (int64, error) {
- result := db.WithContext(ctx).Unscoped().Where("tenant_id = ?", tenantID).Delete(&entity.UserTenant{})
- return result.RowsAffected, result.Error
-}
-
// GetByUserIDAll get all user tenant relationships by user ID (including deleted)
func (dao *UserTenantDAO) GetByUserIDAll(ctx context.Context, db *gorm.DB, userID string) ([]*entity.UserTenant, error) {
var relations []*entity.UserTenant