{"record":{"id":"f0819c6669050d89","repo":"denoland/deno","slug":"invalid-mutation-delete-with-value","errorCode":null,"errorMessage":"Invalid mutation 'delete' with value","messagePattern":"Invalid mutation 'delete' with value","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/kv/01_db.ts","lineNumber":485,"sourceCode":"    for (let i = 0; i < checks.length; ++i) {\n      const check = checks[i];\n      ArrayPrototypePush(this.#checks, [check.key, check.versionstamp]);\n    }\n    return this;\n  }\n\n  mutate(...mutations: Deno.KvMutation[]): this {\n    for (let i = 0; i < mutations.length; ++i) {\n      const mutation = mutations[i];\n      const key = mutation.key;\n      let type: string;\n      let value: RawValue | null;\n      let expireIn: number | undefined = undefined;\n      switch (mutation.type) {\n        case \"delete\":\n          type = \"delete\";\n          if (mutation.value) {\n            throw new TypeError(\"Invalid mutation 'delete' with value\");\n          }\n          break;\n        case \"set\":\n          if (typeof mutation.expireIn === \"number\") {\n            expireIn = mutation.expireIn;\n          }\n          validateExpireIn(expireIn);\n          /* falls through */\n        case \"sum\":\n        case \"min\":\n        case \"max\":\n          type = mutation.type;\n          if (!ObjectHasOwn(mutation, \"value\")) {\n            throw new TypeError(`Invalid mutation '${type}' without value`);\n          }\n          value = serializeValue(mutation.value);\n          break;\n        default:","sourceCodeStart":467,"sourceCodeEnd":503,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/kv/01_db.ts#L467-L503","documentation":"In AtomicOperation.mutate(), a mutation with type 'delete' must not carry a value — deletion has no payload. The source checks truthiness (`if (mutation.value)`), so any truthy value on a delete mutation throws TypeError(\"Invalid mutation 'delete' with value\"). The dedicated helpers delete()/set() on the builder cannot hit this; only raw mutate() objects can.","triggerScenarios":"`kv.atomic().mutate({ type: \"delete\", key, value: someValue })` — typically when mutations are built generically (spreading a record that always includes value) or when a set-mutation shape is ported to delete without dropping the value field.","commonSituations":"Generic batch-sync code that maps rows to mutations and attaches value unconditionally; diff engines emitting {type, value} pairs for both inserts and deletes; API layers accepting one mutation shape for all operations.","solutions":["Omit the value property entirely for delete mutations: { type: \"delete\", key }.","Prefer the builder helpers .delete(key) and .set(key, value) whose signatures make the mistake impossible.","Split batches into set-mutations and delete-mutations instead of forcing one shape-for-all.","Type mutation arrays as Deno.KvMutation[] so TypeScript flags the stray value."],"exampleFix":"// before\nops.push({ type, key, value }); // throws when type === \"delete\"\n\n// after\nops.push(type === \"delete\" ? { type, key } : { type, key, value });","handlingStrategy":"validation","validationCode":"function toMutation(row: { type: string; key: Deno.KvKey; value?: unknown }) {\n  if (row.type === \"delete\") {\n    const { type, key } = row; // strip value for deletes\n    return { type, key } as const;\n  }\n  if (!(\"value\" in row)) throw new TypeError(`mutation ${row.type} needs a value`);\n  return { type: row.type, key: row.key, value: row.value } as const;\n}","typeGuard":"const isDeleteMutation = (m: Deno.KvMutation): m is { type: \"delete\"; key: Deno.KvKey } =>\n  m.type === \"delete\";","tryCatchPattern":null,"preventionTips":["Build delete mutations as exactly { type: \"delete\", key }","Prefer the builder helpers .set()/.delete() over raw mutate() where possible","Type batch arrays as Deno.KvMutation[] so shapes are checked"],"tags":["deno-kv","atomic","mutate","validation"],"backgroundTag":"invalid-mutation","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}