plandex-ai/plandex · error

Error deleting contexts:

Error message

Error deleting contexts: 

What it means

The delete operation wrapped in db.ExecRepoOperation inside DeleteContextHandler returned an error, so the handler responds with HTTP 500. The callback fetches plan contexts, removes the requested ones via db.ContextRemove, and commits with repo.GitAddAndCommit — any of these failing (or the lock/ExecRepoOperation itself) surfaces here, and ClearRepoOnErr resets the repo.

Source

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

		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
	})

	if err != nil {
		log.Printf("Error deleting contexts: %v\n", err)
		http.Error(w, "Error deleting contexts: "+err.Error(), http.StatusInternalServerError)
		return
	}

	err = db.AddPlanContextTokens(planId, branchName, -removeTokens)
	if err != nil {
		log.Printf("Error updating plan tokens: %v\n", err)
		http.Error(w, "Error updating plan tokens: "+err.Error(), http.StatusInternalServerError)
		return
	}

	res := shared.DeleteContextResponse{
		TokensRemoved: removeTokens,
		TotalTokens:   branch.ContextTokens - removeTokens,
		Msg:           commitMsg,
	}

	bytes, err := json.Marshal(res)

View on GitHub (pinned to e2d772072e)

Solutions

  1. Read the wrapped error text ('error getting contexts', 'error removing contexts', 'error committing changes') to identify the failing step
  2. Refresh the plan's context list and drop already-deleted IDs from the request
  3. Remove a stale .git/index.lock in the plan repo if a crashed writer left one
  4. Retry after resolving concurrent-writer conflicts; the repo was already cleared on error
  5. Check DB connectivity and disk space for the plan storage backend

Example fix

// before
dbContexts, err = db.GetPlanContexts(auth.OrgId, planId, false, false)
if err != nil {
    return fmt.Errorf("error getting contexts: %v", err)
}
// after
dbContexts, err = db.GetPlanContexts(auth.OrgId, planId, false, false)
if err != nil {
    return fmt.Errorf("error getting contexts: %w", err)
}
// also: filter requestBody.Ids to ids still present in dbContexts before ContextRemove
Defensive patterns

Strategy: fallback

Validate before calling

// before deleting, verify all requested IDs still exist
current, err := listContexts(planId, branchName)
if err != nil { return err }
for id := range req.Ids {
    if !hasContext(current, id) {
        delete(req.Ids, id) // skip already-deleted contexts
    }
}

Try / catch

resp, err := sendDelete(req)
if err != nil && resp != nil && resp.StatusCode == 500 {
    // repo cleared on error; refresh state and retry once with surviving IDs
    if err := refreshPlanState(planId, branchName); err != nil {
        return err
    }
    return retryOnce(req)
}

Prevention

When it happens

Trigger: db.GetPlanContexts fails, db.ContextRemove fails (rows already deleted, DB error), or repo.GitAddAndCommit fails (stale index.lock, conflict, disk full) inside the write-locked callback; ExecRepoOperation's lock/cancel handling can also return an error that lands on this check.

Common situations: Deleting context IDs that another session already deleted; concurrent writers contending on the repo lock; leftover .git/index.lock after a crash; branch deleted mid-request; DB/storage outages.

Related errors


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