bytebase/bytebase · error

target instance %q has been deleted

Error message

target instance %q has been deleted

What it means

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.

Source

Thrown at backend/runner/taskrun/running_scheduler.go:276

	}

	// Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED)
	s.bus.PlanCompletionCheckChan <- bus.PlanRef{ProjectID: task.ProjectID, PlanID: task.PlanID}
}

// validateTaskFreshness checks for state drift between task creation and execution time.
// Returns an error if the target instance has been archived or deleted, the target
// database has been deleted, its project has changed, or its environment has
// changed since the task was created.
func (s *Scheduler) validateTaskFreshness(ctx context.Context, task *store.TaskMessage) error {
	// Every task targets an instance; a task run must not newly start while its
	// target instance is archived. Already-started task runs are not affected.
	instance, err := s.store.GetInstanceByResourceID(ctx, task.InstanceID)
	if err != nil {
		return errors.Wrapf(err, "failed to get instance for drift validation")
	}
	if instance == nil {
		return errors.Errorf("target instance %q has been deleted", task.InstanceID)
	}
	if instance.Deleted {
		return errors.Errorf("target instance %q has been archived", task.InstanceID)
	}

	// DATABASE_CREATE tasks have DatabaseName = nil — the database doesn't exist yet.
	if task.Type == storepb.Task_DATABASE_CREATE {
		return nil
	}

	database, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{
		InstanceID:   &task.InstanceID,
		DatabaseName: task.DatabaseName,
	})
	if err != nil {
		return errors.Wrapf(err, "failed to get database for drift validation")
	}
	if database == nil {

View on GitHub (pinned to 1870550677)

Solutions

  1. Recreate/re-register the target instance if deletion was unintended
  2. Cancel or discard the stale task and create a new one against an existing instance
  3. Verify instanceID in the task matches an existing instance resource ID
  4. Prevent deletion of instances with active pending tasks in your workflow

Example fix

// before
if instance == nil {
	return errors.Errorf("target instance %q has been deleted", task.InstanceID)
}
// after
if instance == nil {
	return errors.Wrapf(store.ErrInstanceNotFound, "target instance %q has been deleted", task.InstanceID)
}
Defensive patterns

Strategy: validation

Validate before calling

inst, err := store.GetInstanceByResourceID(ctx, task.InstanceID)
if err != nil { return err }
if inst == nil || inst.Deleted {
	return fmt.Errorf("instance %s unavailable", task.InstanceID)
}

Type guard

func instanceUsable(inst *store.InstanceMessage) bool { return inst != nil && !inst.Deleted }

Try / catch

err := s.validateTaskFreshness(ctx, task)
var notFoundErr *ErrTargetDeleted
if errors.As(err, &notFoundErr) {
	return cancelTaskRun(task, "target instance deleted")
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/5e29f9748e15c44e. Report an issue: GitHub.