# Erase a person's data

Source: https://docs.littlebigbrain.com/guides/erase-a-person/

A graph keeps its history. A read with `as_of_commit_seq` returns the graph as
it was at that commit. When a person withdraws consent, a retraction alone is
not enough: the old commits still hold what they said. An erasure removes the
person from the current graph and from every earlier commit.

An erasure has two phases:

1. **Mask.** One commit retracts every statement that names the person. From
   that commit on, every read hides those statements, at every
   `as_of_commit_seq`, on every node.
2. **Purge.** A background job rewrites the history, the search indexes and
   the embeddings without the person, and then deletes the old objects. The
   purge completes before the erasure's `deadline`, at most 30 days after the
   request, backups included.

An erasure cannot be undone.

## What an erasure removes

You name subjects by IRI, files by SHA-256 hash, or both. The erasure removes:

- every statement in which a named subject is the subject or the object;
- the statement nodes about those statements;
- the nodes that a `follow` rule reaches;
- the named files: their `Document` and content records, and their bytes,
  pages and derived outputs;
- the literals that only those nodes use.

The erasure does not search text. A record that mentions the person by name,
with no link to the person, stays. To remove it, name it, or link it to the
person and send a `follow` rule.

A `follow` rule adds the nodes linked to the set by one predicate:

- `"direction": "in"` adds every `?node` of `?node <predicate> <member>`.
- `"direction": "out"` adds every `?node` of `<member> <predicate> ?node`.

Rules repeat up to 3 steps. In this example each message links to its author
with `ex:author`, and each quote links to its message with `ex:quotes`:

```json
{
  "subjects": ["https://example.com/person/ana"],
  "follow": [
    { "predicate": "https://example.com/author", "direction": "in" },
    { "predicate": "https://example.com/quotes", "direction": "in" }
  ]
}
```

The first rule adds Ana's messages. The second step adds the quotes of those
messages.

## Preview the erasure

A dry run computes the scope at the head and writes nothing. It returns the
counts, up to 50 of the statements to remove, and the shape results the
removal would create.

```python
import os

from lbb import LbbClient

lbb = LbbClient(os.environ["LBB_URL"], api_key=os.environ["LBB_API_KEY"], graph="support")
scope = {
    "subjects": ["https://example.com/person/ana"],
    "follow": [{"predicate": "https://example.com/author", "direction": "in"}],
}
preview = lbb.erasures.preview(**scope)
print(preview["counts"])  # {'subjects': 1, 'followed': 12, 'statements': 96, ...}
```

```ts
const preview = await lbb.erasures.preview(scope);
console.log(preview.counts);
```

Over HTTP, send `POST /v1/erasures` with `"dry_run": true`.

## Erase

```python
erasure = lbb.erasures.erase(**scope, idempotency_key="erasure-request-4821")
print(erasure["id"], erasure["status"])  # ers_… masked
```

```ts
const erasure = await lbb.erasures.erase(scope, {
  idempotencyKey: "erasure-request-4821",
});
```

The request needs an `Idempotency-Key`. The SDKs send one. Pass your own, for
example the id of the person's request, so that a rerun of your code returns
the first erasure with `idempotent_replay: true`.

When the call returns, the erasure is `masked`: no read returns the person's
statements. The erasure object counts what it removed and never names the
subjects.

## Follow the purge

```python
lbb.erasures.get(erasure["id"])["status"]
lbb.erasures.list()["data"]
```

The `status` moves through these values:

| Status | Meaning |
| --- | --- |
| `masking` | The erase commit is in progress. |
| `masked` | Every read hides the scope. The purge has not started. |
| `purging` | The purge rewrites history and indexes. |
| `verifying` | The purge checks that nothing of the scope remains. |
| `complete` | The scope is gone from storage. |
| `failed` | The erase commit failed. `failure` says why. Nothing was masked. |

`deadline` is the time, in Unix seconds, by which the purge completes.

## Write the same IRI again

While an erasure runs, a write that names an erased IRI is refused with
`400 subject_erased`. When the purge is `complete`, the IRI is released. A new
write of the same IRI is then new data, with no link to the erased one.

## Shapes

An erasure can break a shape on a node that stays. For example, a project can
lose its only lead. The request then fails with `409 erasure_shape_conflict`
and writes nothing. The dry run lists the focus nodes, shapes and paths.
Widen the scope, change those nodes first, or send the request again with
`"accept_shape_violations": true`.

## Limits

- An erasure applies to one graph. Erase each graph that holds the person.
- Property graphs and graphs with record support are refused with
  `400 erasure_unsupported`.
- While a fork of the graph runs, the request is refused with
  `409 erasure_fork_in_progress`. A graph with an erasure in progress cannot
  be forked (`400 erasure_fork_unsupported`).
- A scope too large for one commit is refused with
  `400 erasure_scope_too_large`. Split it into several requests.
- The MCP server has no erasure tool. Erase through the SDKs or HTTP.

See [Error codes](https://docs.littlebigbrain.com/sdks/errors/) for every erasure code, and
[Replay answers & history](https://docs.littlebigbrain.com/guides/time-travel-audit/) for reads at an earlier
commit.
