Skip to Content
Meubles Morinière

Engineering choicesTechnical case studies6 min read

An erasure that survives a restore

The right to erasure, the retention of accounting records and disaster recovery don’t pull the same way. An erased person had to stay gone when a copy of the database comes back, without their address being kept anywhere.

The problem

A customer can ask for their data to be erased, or delete their account on the storefront. The ERP does it in the database: accounts are deleted, orders and quotes are anonymised. Except a database gets restored. After a failed migration or a handling mistake, you start again from an older copy. And everything erased since that copy comes back: the name, address, phone number and purchase history of someone who asked to disappear.

Erasing isn’t the hard part. The hard part is remembering that you erased, because a restore rolls back exactly that, the system’s memory.

The constraints

  • The law also requires keeping things. Accounting records are kept for their full legal period, so an invoiced order can’t disappear. It’s anonymised: its amounts stay and its identity goes. The periods are in packages/domain/src/data-retention.ts, and the storefront’s privacy policy takes them from the same constant (Personal data).
  • You can’t edit a copy. The manual copy is a dump encrypted with AES-256-GCM. Neon’s history, which is used for point-in-time recovery, can’t be rewritten. So there’s no going back to erase someone from the copies that already exist.
  • The list of erasures must not become a customer file. If you kept the erased addresses in plain text somewhere to remember them, you’d recreate the data you just deleted.
  • The storefront has no control over the ERP. An account deletion starts on the storefront, and the storefront has neither the ERP’s rights on the database nor the key to the document store (The storefront only has the rights it needs).

The alternatives

  • Purge the copies. That’s impossible, both for an encrypted offline copy and for Neon’s history.
  • Keep copies for less time, so an erased person disappears from them quickly. But the recovery window shrinks by the same amount. And a restore made within that window still brings the person back.
  • Write an erasure log in the database. The restore rolls it back with everything else, so it disappears right when you need it.
  • Keep a log of erased addresses outside the database. It survives, but it’s a copy of the data we promised to erase.
  • Keep a register of hashes outside the database. It survives the restore. And it only lets you recognise an address you already know, which is exactly the situation of a restored database.

The decision

The erasure register is outside the database, in the private document store, next to the archived invoices. It holds record identifiers and hashes, but no address. A restore run with --replay-erasures replays it before the copy is reopened. And it refuses to hand back a copy in which an erasure couldn’t be redone. Without the option, db:restore replays nothing.

The implementation

Two kinds of entries

Each entry is a JSON file (apps/erp/shared/lib/erasure-register.ts):

  • The erasure of an order, under erasures/: its identifier, the instant, the reason.
  • The erasure of a person, under erasures/subjects/: their customer record or the hash of their address, the instant, the reason (the person asked for it, or they deleted their account on the storefront).

A hash that can be recomputed on both sides

The hash is the SHA-256 of the address with no spaces or capitals (emailFingerprint, packages/contracts/src/erasure.ts). On replay, the restored database recomputes the same hash in SQL over the addresses it knows (accounts, quotes, orders, customer records). It finds the person even though the register never named them.

emailFingerprintpackages/contracts/src/erasure.ts · lines 5–11
export async function emailFingerprint(email: string): Promise<string> {
	const digest = await crypto.subtle.digest(
		"SHA-256",
		new TextEncoder().encode(email.trim().toLowerCase()),
	);
	return toHex(new Uint8Array(digest));
}

A replay bounded in time

An entry only erases what existed at the time of the erasure (createdAt <= erasedAt). If someone comes back to order after being erased, the next restore doesn’t erase them a second time.

Anonymise what the law keeps, delete the rest

The replay replaces the identity of orders, refunds, parcels and accepted quotes with the neutral values from the code (ANONYMIZED_ORDER_DATA and its neighbours). It deletes customer accounts, and it detaches or deletes quotes that weren’t accepted. It only anonymises the customer record once nothing is attached to it anymore.

If an erasure doesn’t go through, the restore fails

pnpm db:restore --replay-erasures (apps/erp/scripts/restore-database.mts) restores into an empty database, then replays the register. Each entry ends up in one of four columns: redone, already in place, absent from the copy, refused. If an entry is refused by a database constraint, or if it’s unreadable, the restore fails: “this copy must not be reopened before they are handled by hand”. Reading is strict (exact set of keys, known version). A file whose path doesn’t match its content is rejected.

Several paths fill the register

  • In the store, the anonymisation is written to the database first, which is the reference, then to the register. If the write to the register fails, the screen says so and an alert goes out. For an order, the next daily check rewrites the entry that’s missing from the register (reconcileErasureRegister). For a person (their customer record, quotes, account), the register is written before the database, and a failed entry isn’t caught up on its own: the alert asks for the erasure to be redone by hand before a restore.
  • On the storefront, when an account is deleted, the ERP is notified with a signed event (HMAC over the timestamp and the body, valid for five minutes), and it’s the ERP that writes the hash. This channel has its own secret. Whoever had it could get any address entered in the register (README).
  • The copy of the accounting documents (pnpm db:backup-documents) also takes the register with it (MIRRORED_PREFIXES, scripts/lib/documents-mirror.ts).

The results

  • An erased person doesn’t come back with a copy restored by db:restore --replay-erasures. And a copy in which an erasure fails isn’t handed back. The restore log (restore.erasures) says how many entries were redone, how many were already in place and how many targeted records that were absent.
  • No address is kept in order to remember. The register only holds identifiers and hashes.
  • On the testing side, erasure-register.regression.test.ts checks that writing, reading and replay agree. replayed-erasures.db.test.ts and erasure-register-reconciliation.db.test.ts run the replay and the catch-up on a real database. The register’s unit tests check that the replay is limited to records created before the erasure.

The limits

  • Neon’s point-in-time recovery doesn’t go through this replay. And yet it’s prod’s default rollback, now that migrations run without a copy (--sans-sauvegarde, Backups and recovery). As of November 13, 2026, only db:restore replays the register, and it requires an empty database. So a database taken back to a point in time by Neon brings back the people erased since that point, until someone replays the register by hand. A tool that replays the register on an already restored database is missing.
  • The hash isn’t anonymous data. It lets you recognise an address you already know. That’s what it’s for, and that’s why the register is in the private store and never in a public repository.
  • The daily check only catches up on per-order entries decided in the ERP. If the event of an account deletion doesn’t reach the ERP, it isn’t recorded. An alert then asks for it to be redone before any restore.
  • The law comes before erasure. The amounts of an invoiced order stay, anonymised, for the full legal period. Only the identity goes.

On this page