{"record":{"id":"3e0746e05104e1a3","repo":"mastra-ai/mastra","slug":"cannot-delete-the-active-version-activate-a-diffe","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/agent-versions.ts","lineNumber":470,"sourceCode":"      // Verify agent exists\n      const agent = await agentsStore.getById(agentId);\n      if (!agent) {\n        throw new HTTPException(404, { message: `Agent with id ${agentId} not found` });\n      }\n      assertStoredResourceScope(agent, await getStoredResourceScope(mastra, requestContext));\n\n      // Verify version exists and belongs to this agent\n      const version = await agentsStore.getVersion(versionId);\n      if (!version) {\n        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });\n      }\n      if (version.agentId !== agentId) {\n        throw new HTTPException(404, { message: `Version with id ${versionId} not found for agent ${agentId}` });\n      }\n\n      // Check if this is the active version\n      if (agent.activeVersionId === versionId) {\n        throw new HTTPException(400, {\n          message: 'Cannot delete the active version. Activate a different version first.',\n        });\n      }\n\n      await agentsStore.deleteVersion(versionId);\n\n      // Clear the editor cache in case the deleted version affected resolution\n      mastra.getEditor()?.agent.clearCache(agentId);\n\n      return {\n        success: true,\n        message: `Version ${version.versionNumber} deleted successfully`,\n      };\n    } catch (error) {\n      return handleError(error, 'Error deleting agent version');\n    }\n  },\n});","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/server/src/server/handlers/agent-versions.ts#L452-L488","documentation":"The version is valid but is the agent's currently active version (agent.activeVersionId === versionId). Deleting the active version would leave the agent without a runnable definition, so the endpoint rejects the request with a 400 and asks you to activate another version first.","triggerScenarios":"Calling the delete-version endpoint while passing the versionId that equals the agent's activeVersionId — typically deleting 'latest' or the only remaining version of an agent.","commonSituations":"Cleanup scripts that iterate all versions without skipping the active one; UI flows where the user selects the current version; agents with a single version where every version is active.","solutions":["Activate a different version first (POST activate endpoint), then delete the previously active one","Skip the active version in bulk delete scripts","If you want the agent gone entirely, delete the agent rather than its active version","Create and activate a new version, then delete the old one"],"exampleFix":"// before\nawait fetch(`/api/agents/${agentId}/versions/${activeVersionId}`, { method: 'DELETE' });\n// after\nawait fetch(`/api/agents/${agentId}/versions/${otherVersionId}/activate`, { method: 'POST' });\nawait fetch(`/api/agents/${agentId}/versions/${activeVersionId}`, { method: 'DELETE' });","handlingStrategy":"validation","validationCode":"const agent = await (await fetch(`/api/agents/${agentId}`)).json();\nif (agent.activeVersionId === versionId) {\n  throw new Error('Refusing to delete the active version; activate another version first');\n}","typeGuard":"function isDeletable(versionId: string, agent: { activeVersionId: string | null }): boolean {\n  return agent.activeVersionId !== versionId;\n}","tryCatchPattern":"try {\n  const res = await fetch(`/api/agents/${agentId}/versions/${versionId}`, { method: 'DELETE' });\n  if (res.status === 400 && (await res.json()).message.startsWith('Cannot delete the active version')) {\n    // activate a different version, then retry the delete\n  }\n} catch (err) { /* network */ }","preventionTips":["Skip the active version in cleanup/bulk-delete scripts","Always activate a replacement before retiring the current version","Surface activeVersionId in UI delete confirmations","Delete the whole agent instead of its only version when intent is removal"],"tags":["http-400","validation","server","state-conflict"],"backgroundTag":"active-version-delete-conflict","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}