{"record":{"id":"5e29f9748e15c44e","repo":"bytebase/bytebase","slug":"target-instance-q-has-been-deleted","errorCode":null,"errorMessage":"target instance %q has been deleted","messagePattern":"target instance %q has been deleted","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/runner/taskrun/running_scheduler.go","lineNumber":276,"sourceCode":"\t}\n\n\t// Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED)\n\ts.bus.PlanCompletionCheckChan <- bus.PlanRef{ProjectID: task.ProjectID, PlanID: task.PlanID}\n}\n\n// validateTaskFreshness checks for state drift between task creation and execution time.\n// Returns an error if the target instance has been archived or deleted, the target\n// database has been deleted, its project has changed, or its environment has\n// changed since the task was created.\nfunc (s *Scheduler) validateTaskFreshness(ctx context.Context, task *store.TaskMessage) error {\n\t// Every task targets an instance; a task run must not newly start while its\n\t// target instance is archived. Already-started task runs are not affected.\n\tinstance, err := s.store.GetInstanceByResourceID(ctx, task.InstanceID)\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to get instance for drift validation\")\n\t}\n\tif instance == nil {\n\t\treturn errors.Errorf(\"target instance %q has been deleted\", task.InstanceID)\n\t}\n\tif instance.Deleted {\n\t\treturn errors.Errorf(\"target instance %q has been archived\", task.InstanceID)\n\t}\n\n\t// DATABASE_CREATE tasks have DatabaseName = nil — the database doesn't exist yet.\n\tif task.Type == storepb.Task_DATABASE_CREATE {\n\t\treturn nil\n\t}\n\n\tdatabase, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{\n\t\tInstanceID:   &task.InstanceID,\n\t\tDatabaseName: task.DatabaseName,\n\t})\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"failed to get database for drift validation\")\n\t}\n\tif database == nil {","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/bytebase/bytebase/blob/1870550677fe08f0d2a78c07acd27541464eb945/backend/runner/taskrun/running_scheduler.go#L258-L294","documentation":"Thrown when GetInstanceByResourceID returns (nil, nil): the instance the task targets no longer exists in the metadata store. validateTaskFreshness refuses to start a new task run against a deleted instance, returning this error naming the instance resource ID.","triggerScenarios":"A task run transitions from pending to running after its target instance was hard-deleted (not merely archived) — e.g. instance removed by another admin or sync while the task was queued.","commonSituations":"Instance deleted while a deployment/rollout task sat pending; stale tasks referencing removed instances after an instance re-registration; race between instance deletion and task scheduling.","solutions":["Recreate/re-register the target instance if deletion was unintended","Cancel or discard the stale task and create a new one against an existing instance","Verify instanceID in the task matches an existing instance resource ID","Prevent deletion of instances with active pending tasks in your workflow"],"exampleFix":"// before\nif instance == nil {\n\treturn errors.Errorf(\"target instance %q has been deleted\", task.InstanceID)\n}\n// after\nif instance == nil {\n\treturn errors.Wrapf(store.ErrInstanceNotFound, \"target instance %q has been deleted\", task.InstanceID)\n}","handlingStrategy":"validation","validationCode":"inst, err := store.GetInstanceByResourceID(ctx, task.InstanceID)\nif err != nil { return err }\nif inst == nil || inst.Deleted {\n\treturn fmt.Errorf(\"instance %s unavailable\", task.InstanceID)\n}","typeGuard":"func instanceUsable(inst *store.InstanceMessage) bool { return inst != nil && !inst.Deleted }","tryCatchPattern":"err := s.validateTaskFreshness(ctx, task)\nvar notFoundErr *ErrTargetDeleted\nif errors.As(err, &notFoundErr) {\n\treturn cancelTaskRun(task, \"target instance deleted\")\n}","preventionTips":["Cancel pending tasks when their target instance is deleted","Verify the instance exists before creating tasks","Periodically purge tasks referencing archived instances"],"tags":["instance","drift","deleted-resource","scheduler"],"backgroundTag":"resource-not-found","analyzedSha":"1870550677fe08f0d2a78c07acd27541464eb945","analyzedAt":"2026-09-06T21:16:13.665Z","contentChangedAt":"2026-09-06T21:16:13.665Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}