Skip to content

Commit 6c73be8

Browse files
committed
better docs for resetting
1 parent 2b6d6d7 commit 6c73be8

File tree

1 file changed

+108
-10
lines changed

1 file changed

+108
-10
lines changed

docs/idempotency.mdx

Lines changed: 108 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,120 @@ You can reset an idempotency key to clear it from all associated runs. This is u
362362

363363
When you reset an idempotency key, it will be cleared for all runs that match both the task identifier and the idempotency key in the current environment. This allows you to trigger the task again with the same key.
364364

365+
### API signature
366+
367+
```ts
368+
idempotencyKeys.reset(
369+
taskIdentifier: string,
370+
idempotencyKey: IdempotencyKey | string | string[],
371+
options?: {
372+
scope?: "run" | "attempt" | "global";
373+
parentRunId?: string;
374+
attemptNumber?: number;
375+
}
376+
)
377+
```
378+
379+
| Parameter | Description |
380+
| --- | --- |
381+
| `taskIdentifier` | The identifier of the task (e.g., `"my-task"`) |
382+
| `idempotencyKey` | The idempotency key to reset. Can be an `IdempotencyKey` from `create()`, a raw string, or a string array |
383+
| `options.scope` | The scope used when the key was created. Defaults to `"run"` |
384+
| `options.parentRunId` | Required for `"run"` or `"attempt"` scope when called outside a task context |
385+
| `options.attemptNumber` | Required for `"attempt"` scope when called outside a task context |
386+
387+
### Resetting keys created with `idempotencyKeys.create()`
388+
389+
When you pass an `IdempotencyKey` created with `idempotencyKeys.create()`, the scope and original key are automatically extracted, making it easy to reset:
390+
391+
```ts
392+
import { idempotencyKeys, task } from "@trigger.dev/sdk";
393+
394+
export const parentTask = task({
395+
id: "parent-task",
396+
run: async (payload) => {
397+
const key = await idempotencyKeys.create("my-key", { scope: "global" });
398+
await childTask.trigger(payload, { idempotencyKey: key });
399+
400+
// Later in the same task, reset it - options extracted automatically
401+
await idempotencyKeys.reset("child-task", key);
402+
},
403+
});
404+
```
405+
406+
### Resetting global keys
407+
408+
Global keys are the simplest to reset since they don't require any run context:
409+
365410
```ts
366411
import { idempotencyKeys } from "@trigger.dev/sdk";
367412

368-
// Reset an idempotency key for a specific task
369-
await idempotencyKeys.reset("my-task", "my-idempotency-key");
413+
// From anywhere - inside a task or from your backend code
414+
await idempotencyKeys.reset("my-task", "my-key", { scope: "global" });
415+
```
416+
417+
### Resetting run-scoped keys
418+
419+
Keys created with `"run"` scope (the default) include the parent run ID in the hash. When resetting from inside the same task, the run ID is automatically available:
420+
421+
```ts
422+
import { idempotencyKeys, task } from "@trigger.dev/sdk";
423+
424+
export const parentTask = task({
425+
id: "parent-task",
426+
run: async (payload) => {
427+
// Create a run-scoped key (default)
428+
const key = await idempotencyKeys.create("my-key");
429+
await childTask.trigger(payload, { idempotencyKey: key });
430+
431+
// Reset works automatically inside the task - run ID is available from context
432+
await idempotencyKeys.reset("child-task", key);
433+
},
434+
});
435+
```
436+
437+
When resetting from outside a task (e.g., from your backend code), you must provide the `parentRunId`:
438+
439+
```ts
440+
import { idempotencyKeys } from "@trigger.dev/sdk";
441+
442+
// From your backend code - you need to know the parent run ID
443+
await idempotencyKeys.reset("my-task", "my-key", {
444+
scope: "run",
445+
parentRunId: "run_abc123"
446+
});
447+
```
448+
449+
### Resetting attempt-scoped keys
450+
451+
Keys created with `"attempt"` scope include both the parent run ID and attempt number. When resetting from outside a task, you must provide both:
452+
453+
```ts
454+
import { idempotencyKeys } from "@trigger.dev/sdk";
455+
456+
// From your backend code
457+
await idempotencyKeys.reset("my-task", "my-key", {
458+
scope: "attempt",
459+
parentRunId: "run_abc123",
460+
attemptNumber: 1
461+
});
370462
```
371463

372-
The `reset` function requires both parameters:
373-
- `taskIdentifier`: The identifier of the task (e.g., `"my-task"`)
374-
- `idempotencyKey`: The idempotency key to reset
464+
<Warning>
465+
If you try to reset a `"run"` or `"attempt"` scoped key from outside a task without providing the required `parentRunId` (and `attemptNumber` for attempt scope), it will throw an error.
466+
</Warning>
467+
468+
### Resetting from the dashboard
375469

376-
After resetting, any subsequent triggers with the same idempotency key will create new task runs instead of returning the existing ones.
470+
You can also reset idempotency keys directly from the Trigger.dev dashboard:
471+
472+
1. Navigate to the run that has the idempotency key you want to reset
473+
2. In the run details panel, find the "Idempotency key" section
474+
3. Click the "Reset" button
475+
476+
This is useful when you need to manually allow a task to be re-triggered without writing code.
477+
478+
![Idempotency section in the run details pane showing the key, scope, and expiration time](/images/idempotency-key-dashboard.png)
377479

378480
<Note>
379481
Resetting an idempotency key only affects runs in the current environment. The reset is scoped to the specific task identifier and idempotency key combination.
@@ -397,8 +499,4 @@ The scope determines what gets hashed alongside your key:
397499

398500
This is why understanding scopes is crucial: the same string key can produce different idempotency behavior depending on the scope and context.
399501

400-
### Viewing scope in the dashboard
401-
402-
When you view a run in the Trigger.dev dashboard, you can see both the idempotency key and its scope in the run details panel. This helps you debug idempotency issues by understanding exactly how the key was configured.
403502

404-
![Idempotency section in the run details pane showing the key, scope, and expiration time](/images/idempotency-key-dashboard.png)

0 commit comments

Comments
 (0)