vxcontrol/pentagi · error

failed to delete source directory %q: %w

Error message

failed to delete source directory %q: %w

What it means

The final step of a directory merge removes the source root directory row itself. If that delete fails the error wraps the DB error with the source path in the message and rolls back the whole merge, keeping source and destination consistent.

Source

Thrown at backend/pkg/server/services/resources.go:1116

			}
		}

		updated, err := updateMovedResource(tx, entry, newPath, now)
		if err != nil {
			return result, err
		}
		result.Updated = append(result.Updated, convertResource(updated))
	}

	for _, dir := range sourceDirsToDelete {
		if err := tx.Delete(&dir).Error; err != nil {
			return result, fmt.Errorf("failed to delete merged source directory %q: %w", dir.Path, err)
		}
		result.DeletedAfter = append(result.DeletedAfter, convertResource(dir))
	}
	if sourceRoot != nil {
		if err := tx.Delete(sourceRoot).Error; err != nil {
			return result, fmt.Errorf("failed to delete source directory %q: %w", sourceRoot.Path, err)
		}
		result.DeletedAfter = append(result.DeletedAfter, convertResource(*sourceRoot))
	}

	return result, nil
}

func listResourceTree(tx *gorm.DB, uid uint64, rootPath string) ([]models.UserResource, error) {
	escapedRoot := resources.EscapeLike(rootPath)
	var entries []models.UserResource
	if err := tx.Where(
		"user_id = ? AND (path = ? OR path LIKE ?)",
		uid, rootPath, escapedRoot+"/%",
	).Order("path ASC").Find(&entries).Error; err != nil {
		return nil, fmt.Errorf("failed to list source directory: %w", err)
	}
	return entries, nil
}

View on GitHub (pinned to ea665308ba)

Solutions

  1. Find what still references the source directory row and clean it up or add cascade
  2. Inspect the wrapped driver error for the specific constraint
  3. Retry the merge after contention/transient DB issues are resolved
Defensive patterns

Strategy: try-catch

Try / catch

err := svc.MoveResource(ctx, uid, srcDir, dstDir, true)
if err != nil && strings.Contains(err.Error(), "failed to delete source directory") {
    // find lingering FK references to the source row via the wrapped error
}

Prevention

When it happens

Trigger: All child entries were merged/deleted but tx.Delete(sourceRoot) fails — typically a remaining FK reference to the root directory row (e.g. logs, tool results pointing at the path), row locks, or connection loss.

Common situations: Implicit references to the source directory stored outside user_resources without cascade; long-running transactions holding locks on the row; DB failover mid-merge.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/3c5098fdc44c4a85. Report an issue: GitHub.