mastra-ai/mastra · error · HTTPException

Cannot delete the active version. Activate a different versi

Error message

Cannot delete the active version. Activate a different version first.

What it means

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.

Source

Thrown at packages/server/src/server/handlers/agent-versions.ts:470

      // Verify agent exists
      const agent = await agentsStore.getById(agentId);
      if (!agent) {
        throw new HTTPException(404, { message: `Agent with id ${agentId} not found` });
      }
      assertStoredResourceScope(agent, await getStoredResourceScope(mastra, requestContext));

      // Verify version exists and belongs to this agent
      const version = await agentsStore.getVersion(versionId);
      if (!version) {
        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.agentId !== agentId) {
        throw new HTTPException(404, { message: `Version with id ${versionId} not found for agent ${agentId}` });
      }

      // Check if this is the active version
      if (agent.activeVersionId === versionId) {
        throw new HTTPException(400, {
          message: 'Cannot delete the active version. Activate a different version first.',
        });
      }

      await agentsStore.deleteVersion(versionId);

      // Clear the editor cache in case the deleted version affected resolution
      mastra.getEditor()?.agent.clearCache(agentId);

      return {
        success: true,
        message: `Version ${version.versionNumber} deleted successfully`,
      };
    } catch (error) {
      return handleError(error, 'Error deleting agent version');
    }
  },
});

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Activate a different version first (POST activate endpoint), then delete the previously active one
  2. Skip the active version in bulk delete scripts
  3. If you want the agent gone entirely, delete the agent rather than its active version
  4. Create and activate a new version, then delete the old one

Example fix

// before
await fetch(`/api/agents/${agentId}/versions/${activeVersionId}`, { method: 'DELETE' });
// after
await fetch(`/api/agents/${agentId}/versions/${otherVersionId}/activate`, { method: 'POST' });
await fetch(`/api/agents/${agentId}/versions/${activeVersionId}`, { method: 'DELETE' });
Defensive patterns

Strategy: validation

Validate before calling

const agent = await (await fetch(`/api/agents/${agentId}`)).json();
if (agent.activeVersionId === versionId) {
  throw new Error('Refusing to delete the active version; activate another version first');
}

Type guard

function isDeletable(versionId: string, agent: { activeVersionId: string | null }): boolean {
  return agent.activeVersionId !== versionId;
}

Try / catch

try {
  const res = await fetch(`/api/agents/${agentId}/versions/${versionId}`, { method: 'DELETE' });
  if (res.status === 400 && (await res.json()).message.startsWith('Cannot delete the active version')) {
    // activate a different version, then retry the delete
  }
} catch (err) { /* network */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/3e0746e05104e1a3. Report an issue: GitHub.