{"record":{"id":"f50bf6cde08a01ad","repo":"mastra-ai/mastra","slug":"knowledge-record-not-found-id","errorCode":null,"errorMessage":"Knowledge record not found: ${id}","messagePattern":"Knowledge record not found: (.+?)","errorType":"exception","errorClass":"KnowledgeNotFoundError","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/knowledge/inmemory.ts","lineNumber":396,"sourceCode":"    const records = [...this.#db.knowledgeRecords.values()]\n      .filter(\n        record =>\n          record.sourceThreadId === input.sourceThreadId &&\n          isKnowledgeScopeVisible(record.scope, scope) &&\n          (input.includeDeleted || !record.deletedAt) &&\n          (!input.after || record.id > input.after),\n      )\n      .sort((left, right) => left.id.localeCompare(right.id))\n      .slice(0, limit + 1);\n    return {\n      records: records.slice(0, limit).map(cloneRecord),\n      nextCursor: records.length > limit ? records[limit - 1]?.id : undefined,\n    };\n  }\n\n  async removeKnowledge({ id, deletedBy }: { id: string; deletedBy: string }): Promise<KnowledgeRecord> {\n    const record = this.#db.knowledgeRecords.get(id);\n    if (!record) throw new KnowledgeNotFoundError('record', id);\n    if (record.deletedAt) return cloneRecord(record);\n    const updated = { ...record, deletedAt: new Date(), deletedBy };\n    this.#db.knowledgeRecords.set(id, updated);\n    this.#recordActivity('record-deleted', 'record', id, record.scope, record.sourceThreadId);\n    this.#enqueue('record', id, 'delete', updated.deletedAt.toISOString(), record.scope);\n    return cloneRecord(updated);\n  }\n\n  async restoreKnowledge({ id }: { id: string }): Promise<KnowledgeRecord> {\n    const record = this.#db.knowledgeRecords.get(id);\n    if (!record) throw new KnowledgeNotFoundError('record', id);\n    if (!record.deletedAt) return cloneRecord(record);\n    const updated = { ...record, deletedAt: undefined, deletedBy: undefined };\n    this.#db.knowledgeRecords.set(id, updated);\n    this.#recordActivity('record-restored', 'record', id, record.scope, record.sourceThreadId);\n    this.#enqueue('record', id, 'upsert', createKnowledgeUlid(), record.scope);\n    return cloneRecord(updated);\n  }","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/knowledge/inmemory.ts#L378-L414","documentation":"removeKnowledge() looks up the record by ID and throws KnowledgeNotFoundError('record', id) if no such record exists in the in-memory map. Deletion is a soft delete (sets deletedAt/deletedBy and enqueues a delete event); it only operates on records that exist.","triggerScenarios":"Calling removeKnowledge({ id }) with an ID that was never appended, an ID from a different storage instance, or an already-purged record; double-processing a delete queue where the record was removed between calls.","commonSituations":"Workers consuming a queue where another worker already handled the record; tests asserting delete on fixtures from a prior store; UI holding a stale list while another session deleted the record.","solutions":["Verify the record exists (listKnowledge) before deleting, or treat not-found as already-deleted.","Make the delete flow idempotent: catch KnowledgeNotFoundError and continue.","Confirm the ID belongs to this storage instance/environment.","If records were merged, delete via the surviving (terminal) node's records instead of stale IDs."],"exampleFix":"// before\nawait storage.removeKnowledge({ id, deletedBy: user });\n// after\ntry {\n  await storage.removeKnowledge({ id, deletedBy: user });\n} catch (e) {\n  if (!(e instanceof KnowledgeNotFoundError)) throw e; // already gone — ok\n}","handlingStrategy":"try-catch","validationCode":"const records = await storage.listKnowledge({});\nconst exists = records.records.some(r => r.id === id && !r.deletedAt);\nif (!exists) return; // nothing to delete","typeGuard":null,"tryCatchPattern":"try {\n  await storage.removeKnowledge({ id, deletedBy });\n} catch (e) {\n  if (e instanceof KnowledgeNotFoundError) return; // already gone\n  throw e;\n}","preventionTips":["Treat delete as idempotent — catch not-found and continue.","Refresh record lists before batch deletes; don't cache IDs across sessions.","Deduplicate delete queues so two workers don't target the same record.","Remember in-memory stores reset on restart; IDs from prior runs are invalid."],"tags":["knowledge","not-found","delete","idempotency"],"backgroundTag":"entity-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}