# After the person's own commit

Erasing a person is one transaction in their store. Everything else an erasure
reaches follows it, and what it doesn't rewrite is kept erased.

## Records in another store

An action can write to only one store, so records your app keeps in another
store are erased straight after the person's own commit, by an event
[subscriber](/docs/subscribers.md) that Jadpo generates, named
`erasure in archive` here. Such a record refers to the person with a
reference that says `erase:` and has no `on_delete:`, and `erase:` means what
it means on any reference:

```jadpo
// app.jadpo
application {
    connections: {
        main: {
            driver: Driver.postgres
        }
        archive: {
            driver: Driver.postgres
        }
        erasures: {
            driver: Driver.postgres
        }
    }
    privacy: {
        erasure_log: {
            store: erasures
        }
    }
}
```

```jadpo
// domains/club/entities/old_ticket.jadpo
use club/entities { Member }
use club/types { TicketTitle }

/// A ticket from years ago, deleted with whoever raised it.
entity OldTicket {
    id: Uuid
    owner_id: Member.id {
        erase: Erasure.deleted
    }
    title: TicketTitle

    store: archive

    policy: {
        create: [Access.public]
        read: [Access.public]
    }
}
```

- **It isn't a foreign key,** since no database holds one across stores, so
  nothing checks what it names when a record is written. It is indexed like
  any reference, since erasure finds its records by it.
- **Nothing reads through it.** A query is one statement, which reads one
  database, so an include can't follow it, and neither can a membership's
  `member:`.
- **Only erasure applies it.** Anything else that would delete the person
  would leave a record holding an `id` no record has, out of reach of any
  erasure, so it is refused: a hard `delete` of them, a `retention:` or a
  `purge:`, on their entity or through a cascade
  ([`data.person_removal_across_stores`](/docs/errors/data.person_removal_across_stores.md)).
  A lifecycle's `delete` only hides them, and erasure still finds them. So a
  person whose own record stays until its retention ends can't be referred
  to from another store.
- **Each store's share is one statement.** Records there it deletes, however
  deep, and those it keeps, go as they would in the person's own store. A
  person it deletes there is erased in their own right, with their own
  [`PersonErased`](/docs/erasure-after-the-commit.md#who-was-erased-personerased) and their own entry in the
  log, so a record in a third store that refers to them is reached from
  there.
- **Any other reference across stores,** with an `on_delete:` or to an entity
  that isn't a person, is
  [`data.reference_across_stores`](/docs/errors/data.reference_across_stores.md).

A store that can't do its share yet doesn't undo the erasure. The delivery
stays in the person's store, tried again for a month, and
[`jadpo deliveries`](/docs/what-an-operator-sees.md) shows it with
why. Past that it is `failed`, and stays for an operator, whose retry finishes
it. The next start's replay applies it too ([What erasure doesn't
rewrite](/docs/erasure-after-the-commit.md#what-erasure-doesnt-rewrite)), once the person's entry is in the log.

This test closes an account and looks in the archive:

```jadpo
// domains/club/actions/close_account.jadpo
use club/entities { Member, MemberNotFound }

/// Erases a member, here and in the archive.
action close_account(member: Member.id) fails MemberNotFound -> Unit {
    attempt Member.erase(member)
}
```

```jadpo
// domains/club/actions/close_account.test.jadpo
use club/entities { Member, MemberNotFound, OldTicket, OldTicketNotFound }
use club/actions { enrol, file_ticket }

test "closing an account reaches the archive" {
    var ada: Member = attempt enrol("ada@example.com")
    var old_ticket: OldTicket = file_ticket(ada, "The heating")
    attempt close_account(ada.id)
    assert Member.get(ada.id) fails MemberNotFound
    assert OldTicket.get(old_ticket.id) fails OldTicketNotFound
}
```

## Who was erased: `PersonErased`

An erasure emits a `PersonErased` event for each person it erases, in its
transaction: `entity`, the person entity's name, `id`, their `id`, and
`erased_at`, when they were erased, and nothing personal. It travels through
the outbox like any event
([Delivery: at least once, safe to repeat](/docs/delivery-at-least-once-safe-to-repeat.md)).
Subscribe to it to reach anything Jadpo can't, such as a CRM your app calls
through its own integration:

```jadpo
// domains/club/entities/farewell.jadpo
/// A note that someone has gone, kept without saying who they were.
entity Farewell {
    id: Uuid
    departed: Uuid
    at: Instant

    policy: {
        create: [Access.system]
        read: [Access.public]
    }
}
```

```jadpo
// domains/club/actions/note_farewell.jadpo
use club/entities { Farewell }

/// Notes that someone has gone.
action note_farewell(event: PersonErased) -> Farewell {
    return Farewell.create {
        set: {
            departed: event.id
            at: event.erased_at
        }
    }
}
```

```jadpo
// domains/club/subscribers/say_farewell.jadpo
use club/actions { note_farewell }

/// Notes each person erased.
subscriber say_farewell {
    on: PersonErased
    run: note_farewell(event)
}
```

A subscriber to it in an app with no person is
[`subscriber.event_invalid`](/docs/errors/subscriber.event_invalid.md).

## What erasure reaches

- **Every store and cache.** The main store's records go in one
  transaction, and once it commits every entry every cache holds is
  deleted, before the response goes ([Caches](/docs/caches.md)). Records
  in another store follow straight after
  ([Records in another store](/docs/erasure-after-the-commit.md#records-in-another-store)), each store's
  share clearing the caches again as it commits.
- **Records with a lifecycle** are removed outright, not soft-deleted.
  Your own code can't do that, but erasure, like purge, is an operation
  the compiler generates, so it can
  ([Lifecycle](/docs/lifecycle.md)).
- **Files** attached to erased records are deleted with them
  ([Files](/docs/files.md)).
- **History.** An audited record erasure deletes takes its history with
  it, in the same statement, and one it keeps keeps its history with
  `<erased>` in place of each value that was the person's
  ([Erasure and the records it keeps](/docs/what-an-audit-trail-keeps.md#erasure-and-the-records-it-keeps)).
- **Feature flags.** Their `id` leaves `jadpo_flags` and
  `jadpo_flag_changes`, where a flag named them, with the erasure or
  straight after it when the flags are in another store
  ([The log](/docs/flags-changed-listed-and-removed.md#the-log)).
- **OAuth grants**, the permissions a person gave your app to act for them
  at a provider, are deleted with the person or record that owns them
  ([OAuth grants](/docs/oauth-grants.md)).
  They are revoked at the provider where its package declares how.
- **Other companies.** Data already sent to a provider, such as an email
  or payment provider, is outside your database. Providers that can erase
  are asked to (see "Erasure at providers" below). The
  [personal-data report](/docs/the-personal-data-report.md) lists every connection that receives a marked field, and whether its
  copy is erased.

## What erasure doesn't rewrite

- **Backups** aren't rewritten. Each erasure leaves an entry in an erasure
  log, holding the person entity's name, the person's `id` and the time
  and nothing else, written after it commits. Your app names where the log
  is kept, in `privacy: { erasure_log: { store: } }`: a store that holds no
  person's records, or a store of files, so restoring a person's database
  can't take the log back with it
  ([`data.erasure_log_store`](/docs/errors/data.erasure_log_store.md)). An app
  that erases someone must name one
  ([`data.erasure_log_required`](/docs/errors/data.erasure_log_required.md)). A
  database named this way needs no entity of its own. A Worker has no disk,
  so it keeps the log in a database or in a `Driver.s3` store of files.
  The runtime replays the log every time it starts, erasing again
  anyone logged who is still there, in each store, so a restored backup is
  erased before the app serves anything. It asks each store about a thousand
  people at a time, and finding nothing changes nothing, however often. A
  log it can't read, or a store it can't work in, keeps the app from serving
  until it can, and a replay that finds someone says so with
  `runtime.erasures_replayed`. An entry stays as long as the log does, since
  a backup older than any entry could still be restored.
  Backups themselves expire on the hosting
  platform's schedule, which the report states. This is what the UK
  regulator, the ICO, calls putting data "beyond use".
- **There are no per-person encryption keys**, so erasure never works by
  deleting a person's key ("crypto-shredding").
- **Logs** aren't rewritten either. They hold identifiers and hashes,
  never marked values, and expire on their own retention.
