denoland/deno · error · TypeError

Invalid mutation 'delete' with value

Error message

Invalid mutation 'delete' with value

What it means

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.

Solutions

  1. Omit the value property entirely for delete mutations: { type: "delete", key }.
  2. Prefer the builder helpers .delete(key) and .set(key, value) whose signatures make the mistake impossible.
  3. Split batches into set-mutations and delete-mutations instead of forcing one shape-for-all.
  4. Type mutation arrays as Deno.KvMutation[] so TypeScript flags the stray value.

Example fix

// before
ops.push({ type, key, value }); // throws when type === "delete"

// after
ops.push(type === "delete" ? { type, key } : { type, key, value });
Defensive patterns

Strategy: validation

Validate before calling

function toMutation(row: { type: string; key: Deno.KvKey; value?: unknown }) {
  if (row.type === "delete") {
    const { type, key } = row; // strip value for deletes
    return { type, key } as const;
  }
  if (!("value" in row)) throw new TypeError(`mutation ${row.type} needs a value`);
  return { type: row.type, key: row.key, value: row.value } as const;
}

Type guard

const isDeleteMutation = (m: Deno.KvMutation): m is { type: "delete"; key: Deno.KvKey } =>
  m.type === "delete";

Prevention

When it happens

Trigger: `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.

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

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/f0819c6669050d89. Report an issue: GitHub.

Appendix: source

Thrown at ext/kv/01_db.ts:485

    for (let i = 0; i < checks.length; ++i) {
      const check = checks[i];
      ArrayPrototypePush(this.#checks, [check.key, check.versionstamp]);
    }
    return this;
  }

  mutate(...mutations: Deno.KvMutation[]): this {
    for (let i = 0; i < mutations.length; ++i) {
      const mutation = mutations[i];
      const key = mutation.key;
      let type: string;
      let value: RawValue | null;
      let expireIn: number | undefined = undefined;
      switch (mutation.type) {
        case "delete":
          type = "delete";
          if (mutation.value) {
            throw new TypeError("Invalid mutation 'delete' with value");
          }
          break;
        case "set":
          if (typeof mutation.expireIn === "number") {
            expireIn = mutation.expireIn;
          }
          validateExpireIn(expireIn);
          /* falls through */
        case "sum":
        case "min":
        case "max":
          type = mutation.type;
          if (!ObjectHasOwn(mutation, "value")) {
            throw new TypeError(`Invalid mutation '${type}' without value`);
          }
          value = serializeValue(mutation.value);
          break;
        default:

View on GitHub (pinned to 9ad36f7a2c)