{"record":{"id":"e5ece832ba43064f","repo":"mastra-ai/mastra","slug":"cannot-delete-the-active-version-activate-a-diffe-e5ece8","errorCode":null,"errorMessage":"Cannot delete the active version. Activate a different version first.","messagePattern":"Cannot delete the active version\\. Activate a different version first\\.","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"packages/server/src/server/handlers/prompt-block-versions.ts","lineNumber":409,"sourceCode":"\n      const promptBlock = await promptBlockStore.getById(promptBlockId);\n      if (!promptBlock) {\n        throw new HTTPException(404, { message: `Prompt block with id ${promptBlockId} not found` });\n      }\n      assertStoredResourceScope(promptBlock, await getStoredResourceScope(mastra, requestContext));\n\n      const version = await promptBlockStore.getVersion(versionId);\n      if (!version) {\n        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });\n      }\n      if (version.blockId !== promptBlockId) {\n        throw new HTTPException(404, {\n          message: `Version with id ${versionId} not found for prompt block ${promptBlockId}`,\n        });\n      }\n\n      if (promptBlock.activeVersionId === versionId) {\n        throw new HTTPException(400, {\n          message: 'Cannot delete the active version. Activate a different version first.',\n        });\n      }\n\n      await promptBlockStore.deleteVersion(versionId);\n\n      // Clear the editor cache so subsequent requests see the updated config\n      mastra.getEditor()?.prompt.clearCache(promptBlockId);\n\n      return {\n        success: true,\n        message: `Version ${version.versionNumber} deleted successfully`,\n      };\n    } catch (error) {\n      return handleError(error, 'Error deleting prompt block version');\n    }\n  },\n});","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/prompt-block-versions.ts#L391-L427","documentation":"A deliberate 400 business-rule error: the requested version is the prompt block's activeVersionId, and the API refuses to delete the currently active version. A block must always keep at least one active version, so the caller must switch the active version first.","triggerScenarios":"DELETE .../prompt-blocks/:promptBlockId/versions/:versionId where promptBlock.activeVersionId === versionId.","commonSituations":"Deleting the latest/live version of a prompt block, cleaning up 'unused' versions without noticing one is pinned as active, or automation scripts iterating all versions.","solutions":["Activate a different version first (PUT/POST the activate-version route for another versionId), then delete this one.","Skip the active version in cleanup loops by checking each version's isActive/activeVersionId before deleting.","If the whole block is unwanted, delete the block itself instead of its active version."],"exampleFix":"// before\nawait del(`/prompt-blocks/${blockId}/versions/${activeVersionId}`);\n// after\nawait post(`/prompt-blocks/${blockId}/versions/${otherVersionId}/activate`);\nawait del(`/prompt-blocks/${blockId}/versions/${activeVersionId}`);","handlingStrategy":"validation","validationCode":"const block = await fetch(`/api/prompt-blocks/${promptBlockId}`).then(r => r.json());\nif (block.activeVersionId === versionId) {\n  throw new Error('Activate another version before deleting this one');\n}","typeGuard":"function isDeletable(block: { activeVersionId: string | null }, versionId: string): boolean {\n  return block.activeVersionId !== versionId;\n}","tryCatchPattern":"try {\n  await deleteVersion(promptBlockId, versionId);\n} catch (e) {\n  if (e.status === 400 && e.message.includes('active version')) {\n    await activateVersion(promptBlockId, fallbackVersionId);\n    await deleteVersion(promptBlockId, versionId);\n  } else throw e;\n}","preventionTips":["Always check activeVersionId before deleting any version.","In cleanup scripts, exclude the active version explicitly.","Prefer deleting the whole block when nothing should remain."],"tags":["http-400","business-rule","prompt-blocks","versions"],"backgroundTag":"invalid-state-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}