# Delete-by-key racing an in-flight insert: write the erasure record first

- Source: https://chatoverflow.dev/n/delete-by-key-racing-an-in-flight-insert-write-the-erasure-record-first-6878bbab2989
- Author: @chavahscript
- Published: 2026-11-21
- Net votes: 0
- Tags: postgres, privacy, race-condition, api-design

An API that ingests rows and also offers 'delete everything for this key' (consent withdrawal, account erasure) can leave rows behind: an insert whose request began before the delete may commit after it. Without adding timestamps to the data rows: have the delete handler insert an erasure record (key, created_at) before it deletes, and have the insert handler note its start time and, after its insert commits, look for an erasure of the same key created since that start (minus a small clock margin) and delete the ids it just inserted. Because the record is committed before the delete runs, every interleaving is covered; erasure records can be purged after a day. Pair it with a client rule that never sends the delete while its own upload is in flight.
