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

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.

Source

Thrown at packages/server/src/server/handlers/prompt-block-versions.ts:409

      const promptBlock = await promptBlockStore.getById(promptBlockId);
      if (!promptBlock) {
        throw new HTTPException(404, { message: `Prompt block with id ${promptBlockId} not found` });
      }
      assertStoredResourceScope(promptBlock, await getStoredResourceScope(mastra, requestContext));

      const version = await promptBlockStore.getVersion(versionId);
      if (!version) {
        throw new HTTPException(404, { message: `Version with id ${versionId} not found` });
      }
      if (version.blockId !== promptBlockId) {
        throw new HTTPException(404, {
          message: `Version with id ${versionId} not found for prompt block ${promptBlockId}`,
        });
      }

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

      await promptBlockStore.deleteVersion(versionId);

      // Clear the editor cache so subsequent requests see the updated config
      mastra.getEditor()?.prompt.clearCache(promptBlockId);

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

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Activate a different version first (PUT/POST the activate-version route for another versionId), then delete this one.
  2. Skip the active version in cleanup loops by checking each version's isActive/activeVersionId before deleting.
  3. If the whole block is unwanted, delete the block itself instead of its active version.

Example fix

// before
await del(`/prompt-blocks/${blockId}/versions/${activeVersionId}`);
// after
await post(`/prompt-blocks/${blockId}/versions/${otherVersionId}/activate`);
await del(`/prompt-blocks/${blockId}/versions/${activeVersionId}`);
Defensive patterns

Strategy: validation

Validate before calling

const block = await fetch(`/api/prompt-blocks/${promptBlockId}`).then(r => r.json());
if (block.activeVersionId === versionId) {
  throw new Error('Activate another version before deleting this one');
}

Type guard

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

Try / catch

try {
  await deleteVersion(promptBlockId, versionId);
} catch (e) {
  if (e.status === 400 && e.message.includes('active version')) {
    await activateVersion(promptBlockId, fallbackVersionId);
    await deleteVersion(promptBlockId, versionId);
  } else throw e;
}

Prevention

When it happens

Trigger: DELETE .../prompt-blocks/:promptBlockId/versions/:versionId where promptBlock.activeVersionId === versionId.

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

Related errors


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