<!-- source: https://www.secondlayer.tools/docs/reorgs -->

# Reorgs

A reorg orphans blocks your tables already hold: delete your rows from the fork height, inclusive, then read forward again.

```bash
curl "$SECONDLAYER_API_URL/v1/streams/reorgs?since=2026-08-01T00:00:00Z&limit=1" \
  -H "Authorization: Bearer sk-sl_…"
```

```ts
import { SecondLayer } from "@secondlayer/sdk";

const sl = new SecondLayer({ apiKey: process.env.SECONDLAYER_API_KEY });

const page = await sl.streams.reorgs.list({
  since: "2026-08-01T00:00:00Z",
  limit: 1,
});
```

```json response.json status=200
{
  "reorgs": [
    {
      "id": "9dba566b-8325-45df-bc17-aed6be93ff70",
      "detected_at": "2026-08-01T18:30:11.356245Z",
      "fork_point_height": 8683934,
      "old_index_block_hash": "0x500e56fb65237ebec60a6da210135d9e96c9f62e0ff5a07cc2bbb72e34e5f849",
      "new_index_block_hash": "0x2569032e50f5cc1f783701d992691ab5ab97d56ff4e9a5d58839d5643cc2205a",
      "orphaned_range": { "from": "8683934:0", "to": "8683935:14" },
      "new_canonical_tip": "8683934:0"
    }
  ],
  "next_since": "2026-08-01T18:30:11.356245Z~9dba566b-8325-45df-bc17-aed6be93ff70"
}
```

Keep rows from the orphaned range and your tables describe a chain that no longer exists. Every read envelope carries the same fork in its `reorgs` array: [Cursors and Pages](https://www.secondlayer.tools/docs/cursors.md#the-envelope).

## Pick Your Path

How you read decides how much rollback code you write.

| You read with | What happens on a reorg | You do |
| --- | --- | --- |
| [A consumer](https://www.secondlayer.tools/docs/checkpoints.md) | It rewinds and calls `onReorg` | Delete from the fork height |
| [A sink](https://www.secondlayer.tools/docs/sinks.md#rollback-is-height-stamped) | It deletes the rows and rereads | Nothing |
| [A subgraph](https://www.secondlayer.tools/docs/subgraphs.md) | Rows are rewritten under new ids | Wait a few blocks before moving money |
| [A webhook](https://www.secondlayer.tools/docs/webhooks.md#reorg-rollback) | `chain.reorg.rollback` lists what may have arrived | Undo each entry |
| A plain `list()` | `reorgs[]` names what to undo | Do it by hand |

- Delivery after a rewind is at least once, so keep writes idempotent.
- The fork block itself is re-delivered, which is why the delete is inclusive.

A consumer rewinds under these rules:

| Rule | Detail |
| --- | --- |
| Scope | Only forks at or below your checkpoint roll back; a fork above it has nothing to undo |
| Depth | A rewind deeper than `maxRollbackDepth` (default 1000 blocks) is refused before anything is deleted |
| Idle | On empty pages the loop polls `reorgs.list`, so a fork that lands while idle at the tip is caught, at one extra request per idle poll |
| Genesis | A fork at genesis hands `onReorg` a `null` cursor |

By hand, roll back rows whose cursor falls inside `orphaned_range`, then re-fetch from `new_canonical_tip`. Both bounds are `<block_height>:<event_index>` cursors; on transaction-keyed feeds, compare by the block height. A consumer does all of this for you.

### Check Your Mirror

Compare your stored hashes with the canonical chain any time, and re-sweep from the first block that diverged.

```ts
const { canonical } = await sl.index.canonical({ fromHeight: myLowestHeight });
for (const block of canonical) {
  if (myHashAt(block.block_height) !== block.block_hash) {
    await db.deleteFromHeight(block.block_height);
    break;
  }
}
```

## Skip Them

Rows at or below `tip.finalized_height` never reorg, so a consumer that reads only those has nothing to undo. The tradeoff is freshness: `finalizedOnly` holds delivery until each row is final, which can put you hundreds of blocks behind the tip.

Use it for accounting and reports, and keep the rollback path for anything a user watches live. The option is on the [checkpointed consumer](https://www.secondlayer.tools/docs/checkpoints.md#options).

## Read next

- [Cursors and Pages](https://www.secondlayer.tools/docs/cursors.md)
- [Checkpoints](https://www.secondlayer.tools/docs/checkpoints.md)
- [Sinks](https://www.secondlayer.tools/docs/sinks.md)
- [Subgraphs](https://www.secondlayer.tools/docs/subgraphs.md)
- [Webhooks](https://www.secondlayer.tools/docs/webhooks.md)

## Notes for agents

- Read reorg history with 'GET /v1/streams/reorgs?since=<iso>' or 'sl.streams.reorgs.list({ since })'. 'since' is required; resume from 'next_since'.
- Delete from 'fork_point_height' inclusive. The fork block is re-delivered, and writes must be idempotent.
- finalizedOnly: true skips reorgs by waiting for tip.finalized_height. Do not use it for live views.

## Setup

Ensure setup once, skipping any step already done:
- CLI: `bun add -g @secondlayer/cli`
- Instance: `secondlayer init` writes `INSTANCE_TOKEN`. Loopback `/v1` reads need no key.
- Hosted reads: export `SECONDLAYER_API_KEY` (`sk-sl_*`) or buy credits with `secondlayer credits buy` (`balance`, `refill`), then `secondlayer whoami`
