{"record":{"id":"4ede0732037b67aa","repo":"Tencent/WeKnora","slug":"task-s-in-queue-s-cannot-be-deleted","errorCode":null,"errorMessage":"task %s in queue %s cannot be deleted","messagePattern":"task (.+?) in queue (.+?) cannot be deleted","errorType":"validation","errorClass":null,"httpStatus":400,"severity":"error","filePath":"internal/router/task_inspector.go","lineNumber":673,"sourceCode":"\tif err != nil {\n\t\treturn true, err\n\t}\n\tif task == nil || !task.Allows(types.RuntimeTaskActionRunNow) {\n\t\treturn true, fmt.Errorf(\"task %s in queue %s cannot run now\", taskID, queue)\n\t}\n\treturn true, a.inspector.RunTask(queue, taskID)\n}\n\nfunc (a *asynqTaskInspector) DeleteRuntimeTask(ctx context.Context, queue, taskID string) (bool, error) {\n\tif a == nil || a.inspector == nil {\n\t\treturn false, nil\n\t}\n\ttask, _, err := a.GetRuntimeTask(ctx, queue, taskID)\n\tif err != nil {\n\t\treturn true, err\n\t}\n\tif task == nil || !task.Allows(types.RuntimeTaskActionDelete) {\n\t\treturn true, fmt.Errorf(\"task %s in queue %s cannot be deleted\", taskID, queue)\n\t}\n\treturn true, a.inspector.DeleteTask(queue, taskID)\n}\n\nfunc (a *asynqTaskInspector) ForceDeleteRuntimeTask(ctx context.Context, queue, taskID string) (bool, error) {\n\tif a == nil || a.inspector == nil {\n\t\treturn false, nil\n\t}\n\treturn true, a.inspector.DeleteTask(queue, taskID)\n}\n\n// PurgeArchivedRuntimeTasks clears the whole archived (dead-letter) set for one\n// queue. asynq's DeleteAllArchivedTasks scopes strictly to the archived list,\n// so pending/active/scheduled/retry work is never at risk.\nfunc (a *asynqTaskInspector) PurgeArchivedRuntimeTasks(ctx context.Context, queue string) (int, bool, error) {\n\tif a == nil || a.inspector == nil {\n\t\treturn 0, false, nil\n\t}","sourceCodeStart":655,"sourceCodeEnd":691,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/router/task_inspector.go#L655-L691","documentation":"DeleteRuntimeTask wraps asynq's inspector DeleteTask with an action-permission check. It first resolves the RuntimeTask via GetRuntimeTask and requires that the task's current state allows RuntimeTaskActionDelete. A nil task (not found) or a state that forbids deletion (e.g. the task is currently active) yields this error.","triggerScenarios":"Calling DeleteRuntimeTask(ctx, queue, taskID) when the taskID does not exist in that queue, or the task is in a state such as active/processed where deletion is disallowed by the RuntimeTask action model.","commonSituations":"Double-clicking a delete button so the second call targets an already-deleted task; attempting to delete a task that just transitioned to active; deleting from the wrong queue name.","solutions":["Confirm the task still exists via GetRuntimeTask before deleting and handle the not-found case as success (idempotent delete)","Check task.Allows(types.RuntimeTaskActionDelete) client-side to pre-validate","For tasks in an undeletable active state, wait for completion or use ForceDeleteRuntimeTask","Verify the queue name matches where the task actually lives"],"exampleFix":"// before\nerr := inspector.DeleteRuntimeTask(ctx, queue, taskID) // fails if active or gone\n// after\ntask, _, _ := inspector.GetRuntimeTask(ctx, queue, taskID)\nif task == nil { return nil } // already deleted\nif task.Allows(types.RuntimeTaskActionDelete) {\n    err = inspector.DeleteRuntimeTask(ctx, queue, taskID)\n}","handlingStrategy":"validation","validationCode":"task, _, err := inspector.GetRuntimeTask(ctx, queue, taskID)\nif err != nil { return err }\nif task == nil { return nil } // already deleted: treat as idempotent success\nif !task.Allows(types.RuntimeTaskActionDelete) { return fmt.Errorf(\"task %s not deletable\", taskID) }","typeGuard":"func canDelete(t types.RuntimeTask) bool { return t != nil && t.Allows(types.RuntimeTaskActionDelete) }","tryCatchPattern":"if err := inspector.DeleteRuntimeTask(ctx, queue, taskID); err != nil {\n    if strings.Contains(err.Error(), \"cannot be deleted\") { /* refresh list; maybe use ForceDeleteRuntimeTask */ }\n    return err\n}","preventionTips":["Treat delete as idempotent: a missing task means success","Pre-check Allows(RuntimeTaskActionDelete) before calling","Route active/unstoppable tasks to ForceDeleteRuntimeTask instead of plain delete"],"tags":["asynq","task-queue","permissions","task-state"],"backgroundTag":"task-not-runnable-state","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}