plandex-ai/plandex · error

error deleting custom models: %w

Error message

error deleting custom models: %w

What it means

Wrapped error raised inside the org model-import transaction (CreateCustomModelsHandler) when db.DeleteCustomModels fails while removing org custom models that no longer appear in the submitted config. Because it runs in a db.WithTx closure, the failure aborts the whole upsert/delete transaction and the handler returns 500 'Failed to import custom models/providers/model packs'.

Source

Thrown at app/server/handlers/models.go:311

				return fmt.Errorf("error creating custom model: %w", err)
			}
		}

		for _, provider := range toUpsertCustomProviders {
			if err := db.UpsertCustomProvider(tx, provider); err != nil {
				return fmt.Errorf("error creating custom provider: %w", err)
			}
		}

		for _, modelPack := range toUpsertModelPacks {
			if err := db.UpsertModelPack(tx, modelPack); err != nil {
				return fmt.Errorf("error creating model pack: %w", err)
			}
		}

		if len(toDeleteCustomModelIds) > 0 {
			if err := db.DeleteCustomModels(tx, auth.OrgId, toDeleteCustomModelIds); err != nil {
				return fmt.Errorf("error deleting custom models: %w", err)
			}
		}

		if len(toDeleteCustomProviderIds) > 0 {
			if err := db.DeleteCustomProviders(tx, auth.OrgId, toDeleteCustomProviderIds); err != nil {
				return fmt.Errorf("error deleting custom providers: %w", err)
			}
		}

		if len(toDeleteModelPackIds) > 0 {
			if err := db.DeleteModelPacks(tx, auth.OrgId, toDeleteModelPackIds); err != nil {
				return fmt.Errorf("error deleting model packs: %w", err)
			}
		}

		return nil
	})

View on GitHub (pinned to e2d772072e)

Solutions

  1. Ensure no model pack or default config still references the custom models being deleted (delete/repoint dependent packs first, or add ON DELETE CASCADE to the FK)
  2. Inspect the wrapped %w error in server logs to identify the exact Postgres error (constraint name, deadlock, connection)
  3. Retry the import after resolving conflicts; the whole transaction rolled back so state is unchanged
  4. Check DB connectivity/pool settings if the underlying error is a connection error

Example fix

// before: models referencing a pack fail FK delete
config.CustomModels = remainingModels
client.ImportCustomModels(config)
// after: drop dependent packs in the same payload
config.CustomModelPacks = packsThatDontUseDeletedModels
client.ImportCustomModels(config)
Defensive patterns

Strategy: validation

Validate before calling

// client-side: keep every referenced custom model in the payload
const modelIds = new Set(config.CustomModels.map(m => m.ModelId))
for (const pack of config.ModelPacks ?? []) {
  for (const role of ['planner','coder','summarizer']) {
    const p = pack[role];
    if (p && !modelIds.has(p.modelId)) throw new Error(`pack ${pack.Name} references missing model ${p.modelId}`)
  }
}

Type guard

function hasAllModels(config) {
  const ids = new Set((config.CustomModels ?? []).map(m => String(m.ModelId)))
  return (config.ModelPacks ?? []).every(pack =>
    ['planner','coder','summarizer'].every(role =>
      !pack[role] || ids.has(pack[role].modelId)))
}

Try / catch

try {
  await client.ImportCustomModels(config)
} catch (err) {
  if (String(err.message).includes('error deleting custom models')) {
    // rollback happened; re-add missing models or drop dependent packs, then retry
  }
}

Prevention

When it happens

Trigger: POST of a custom models/providers/model packs config where existing custom models for the org are absent from the payload, and db.DeleteCustomModels(tx, auth.OrgId, toDeleteCustomModelIds) returns a DB error (connection loss, FK constraint from a model still referenced by a model pack, deadlock, tx already aborted).

Common situations: Deleting a custom model that is still referenced by an existing model pack or plan config; Postgres connection pool exhaustion; concurrent config imports from two clients racing on the same org's custom models.

Related errors


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