{"record":{"id":"0ec64ca0af47a485","repo":"mastra-ai/mastra","slug":"all-message-ids-must-be-non-empty-strings","errorCode":null,"errorMessage":"All message IDs must be non-empty strings","messagePattern":"All message IDs must be non-empty strings","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/memory/src/index.ts","lineNumber":2838,"sourceCode":"    }\n\n    if (input.length === 0) {\n      return;\n    }\n\n    messageIds = input.map(item => {\n      if (typeof item === 'string') {\n        return item;\n      } else if (item && typeof item === 'object' && 'id' in item) {\n        return item.id;\n      } else {\n        throw new Error('Invalid input: array items must be strings or objects with an id property');\n      }\n    });\n\n    const invalidIds = messageIds.filter(id => !id || typeof id !== 'string');\n    if (invalidIds.length > 0) {\n      throw new Error('All message IDs must be non-empty strings');\n    }\n\n    const span = this.createMemorySpan('delete', observabilityContext, undefined, {\n      messageCount: messageIds.length,\n    });\n\n    try {\n      const memoryStore = await this.getMemoryStore();\n\n      await memoryStore.deleteMessages(messageIds);\n      if (this.vector) {\n        this.trackVectorCleanup(this.deleteMessageVectors(messageIds));\n      }\n\n      span?.end({ output: { success: true }, attributes: { messageCount: messageIds.length } });\n    } catch (error) {\n      span?.error({ error: error as Error, endSpan: true });\n      throw error;","sourceCodeStart":2820,"sourceCodeEnd":2856,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/index.ts#L2820-L2856","documentation":"After normalization, deleteMessages verifies that every resolved message ID is a non-empty string. IDs that are empty strings, undefined, or non-strings (from objects whose `id` held such a value) trigger this error before any deletion happens.","triggerScenarios":"Passing objects whose `id` is an empty string or undefined (e.g. records not yet persisted); arrays containing '' or null after a prior shape check; template-built IDs that interpolated to empty values.","commonSituations":"Deleting messages fetched from an API where optional `id` fields are missing; optimistic-UI code deleting not-yet-created messages; CSV/import pipelines producing empty IDs.","solutions":["Filter the array to entries with truthy string IDs before calling: items.filter(m => typeof m?.id === 'string' && m.id)","Ensure messages are persisted (and have server-assigned IDs) before attempting deletion","Fix the upstream data source producing empty IDs"],"exampleFix":"// before\nawait memory.deleteMessages(messages); // some messages lack ids\n\n// after\nconst deletable = messages.filter(m => typeof m.id === 'string' && m.id.length > 0);\nawait memory.deleteMessages(deletable);","handlingStrategy":"validation","validationCode":"const deletable = messages.filter((m): m is { id: string } => typeof m.id === 'string' && m.id.length > 0);\nif (deletable.length === 0) return; // nothing valid to delete","typeGuard":"const hasNonEmptyId = (item: string | { id?: string }): item is { id: string } | string =>\n  typeof item === 'string' ? item.length > 0 : typeof item.id === 'string' && item.id.length > 0;","tryCatchPattern":"try {\n  await memory.deleteMessages(messages);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('non-empty strings')) {\n    console.error('Message with empty/missing id:', messages.filter(m => !m.id));\n  }\n  throw err;\n}","preventionTips":["Only delete messages after they are persisted and have server-assigned IDs","Add a shared precondition filter for non-empty string IDs","Log/skip invalid records rather than letting them abort the whole deletion batch"],"tags":["memory","input-validation","api-misuse"],"backgroundTag":"invalid-input-type","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}