{"record":{"id":"40adf7073b11869e","repo":"mastra-ai/mastra","slug":"dataset-not-found-datasetid","errorCode":null,"errorMessage":"Dataset not found: ${datasetId}","messagePattern":"Dataset not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/datasets/inmemory.ts","lineNumber":352,"sourceCode":"  }\n\n  protected async _doDeleteItem({ id, datasetId }: DeleteDatasetItemInput): Promise<void> {\n    const rows = this.db.datasetItems.get(id);\n    if (!rows || rows.length === 0) {\n      return; // no-op if item doesn't exist\n    }\n\n    const currentRow = rows.find(r => r.validTo === null && !r.isDeleted);\n    if (!currentRow) {\n      return; // already deleted\n    }\n    if (currentRow.datasetId !== datasetId) {\n      throw new Error(`Item ${id} does not belong to dataset ${datasetId}`);\n    }\n\n    const dataset = this.db.datasets.get(datasetId);\n    if (!dataset) {\n      throw new Error(`Dataset not found: ${datasetId}`);\n    }\n\n    // Bump version (T3.26)\n    const newVersion = dataset.version + 1;\n    this.db.datasets.set(datasetId, { ...dataset, version: newVersion });\n\n    // T3.9 — close old row\n    currentRow.validTo = newVersion;\n\n    // T3.9 — insert tombstone.\n    // Tenancy is read from the prior current row rather than re-fetched from\n    // the parent dataset (the pattern used by every DB adapter). This is\n    // deliberate and safe: tenancy is immutable post-create on both datasets\n    // and items (see CreateDatasetInput / UpdateDatasetInput in ../../types.ts),\n    // so currentRow.organizationId / currentRow.projectId are guaranteed to\n    // equal dataset.organizationId / dataset.projectId. Keep this branch in\n    // sync with the DB adapters if that invariant ever changes.\n    const now = new Date();","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/datasets/inmemory.ts#L334-L370","documentation":"Thrown after the ownership check when bumping the dataset version during deleteItem: the datasetId no longer resolves in the datasets map. The item row exists, but the parent dataset was deleted concurrently or never existed under that id.","triggerScenarios":"deleteItem({ datasetId, id }) where the dataset was deleted between item creation and the delete call; a typo'd datasetId that happens to match a stale item row; concurrent deletion of the dataset from another request in the same process.","commonSituations":"Test teardown deleting datasets while cleanup code still deletes items; in-memory store restarted (data lost) while the caller cached old ids; race between dataset deletion and item deletion in async code.","solutions":["Confirm the dataset exists first: dataset.get(datasetId) before deleting items.","Reorder logic to delete items before deleting the dataset, or rely on cascade deletion of items.","Refresh dataset/item ids after store restart — in-memory data does not persist.","Serialize concurrent dataset mutations or guard with existence checks in your workflow."],"exampleFix":"// before\nawait storage.datasets.deleteItem({ datasetId, id });\n// after\nconst ds = await storage.datasets.get(datasetId);\nif (ds) await storage.datasets.deleteItem({ datasetId, id });","handlingStrategy":"validation","validationCode":"const ds = await storage.datasets.get(datasetId);\nif (!ds) return; // dataset gone; skip item deletion","typeGuard":"null","tryCatchPattern":"try {\n  await storage.datasets.deleteItem({ datasetId, id });\n} catch (e) {\n  if (String(e).startsWith('Dataset not found')) return; // idempotent cleanup\n  throw e;\n}","preventionTips":["Delete items before deleting their dataset, or rely on cascade.","Re-fetch ids after any store restart — in-memory storage is ephemeral.","Guard concurrent dataset deletions with existence checks."],"tags":["storage","datasets","inmemory","missing-resource"],"backgroundTag":"resource-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}