plandex-ai/plandex · error

error removing contexts: %v

Error message

error removing contexts: %v

What it means

Returned by DeleteContextHandler when db.ContextRemove(auth.OrgId, planId, toRemove) fails after contexts were fetched and filtered. The DB deletion of the selected context rows failed (toRemove may be empty if no requested IDs matched, which some storage layers reject); the wrapped error is surfaced as HTTP 500 'Error deleting contexts'.

Source

Thrown at app/server/handlers/plans_context.go:398

		ClearRepoOnErr: true,
	}, func(repo *db.GitRepo) error {
		var err error
		dbContexts, err = db.GetPlanContexts(auth.OrgId, planId, false, false)

		if err != nil {
			return fmt.Errorf("error getting contexts: %v", err)
		}

		for _, dbContext := range dbContexts {
			if _, ok := requestBody.Ids[dbContext.Id]; ok {
				toRemove = append(toRemove, dbContext)
			}
		}

		err = db.ContextRemove(auth.OrgId, planId, toRemove)

		if err != nil {
			return fmt.Errorf("error removing contexts: %v", err)
		}

		for _, dbContext := range toRemove {
			toRemoveApiContexts = append(toRemoveApiContexts, dbContext.ToApi())
			removeTokens += dbContext.NumTokens
		}

		commitMsg = shared.SummaryForRemoveContext(toRemoveApiContexts, branch.ContextTokens) + "\n\n" + shared.TableForRemoveContext(toRemoveApiContexts)

		err = repo.GitAddAndCommit(branchName, commitMsg)

		if err != nil {
			return fmt.Errorf("error committing changes: %v", err)
		}

		return nil
	})

View on GitHub (pinned to e2d772072e)

Solutions

  1. Log/inspect the wrapped DB error from ContextRemove
  2. Validate requestBody.Ids against existing contexts and return early when toRemove is empty
  3. Check for concurrent deletes and unique/foreign-key constraint violations
  4. Retry transient DB failures

Example fix

// before
err = db.ContextRemove(auth.OrgId, planId, toRemove)
if err != nil {
    return fmt.Errorf("error removing contexts: %v", err)
}
// after
if len(toRemove) == 0 {
    return nil // nothing matched requestBody.Ids; skip remove + commit
}
if err := db.ContextRemove(auth.OrgId, planId, toRemove); err != nil {
    return fmt.Errorf("error removing contexts: %w", err)
}
Defensive patterns

Strategy: validation

Validate before calling

toRemove := make([]*db.Context, 0)
for _, dbContext := range dbContexts {
    if _, ok := requestBody.Ids[dbContext.Id]; ok {
        toRemove = append(toRemove, dbContext)
    }
}
if len(toRemove) == 0 {
    return errors.New("no matching contexts found for requested ids")
}

Try / catch

if err := db.ContextRemove(auth.OrgId, planId, toRemove); err != nil {
    return fmt.Errorf("error removing contexts: %w", err)
}
// caller: inspect wrapped cause; on constraint errors refresh context list and retry once

Prevention

When it happens

Trigger: Deleting contexts whose IDs were found via GetPlanContexts but whose DB removal then fails — storage-layer delete error, transaction failure, or calling ContextRemove with an empty toRemove slice because none of requestBody.Ids matched existing contexts.

Common situations: Client sending stale/unknown context IDs so toRemove is empty; concurrent deletion by another session; DB constraint or connection errors.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/429efa72b610a50c. Report an issue: GitHub.