← Back to SQL, page 18

sql-0352 · Medium mid

A user asks to have all of their personal data deleted. How would you handle that in the database?

Find every table that holds the user's data, using foreign keys and a maintained data inventory. Delete or anonymise rows in a transaction, keeping records that law requires (such as invoices) with personal fields scrubbed. Remember copies outside the main tables: backups, logs, search indexes, caches and analytics stores.

Full answer

Steps:

  1. Inventory: list tables and columns with personal data. Foreign keys help find dependants; free-text columns and JSON need a manual check.
  2. Decide per table: delete (sessions, preferences), anonymise (orders kept for accounting, with name and address replaced), or retain with a legal basis (invoices for tax periods).
  3. Execute in one transaction where possible, with ON DELETE CASCADE only where cascading is clearly intended.
  4. Downstream systems: queue deletions for search indexes, warehouses and third-party processors.
  5. Backups: usually not edited, but expire on a schedule; record the request so restored data is deleted again after a restore.
  6. Record that the request was completed, without keeping the deleted data.

Soft delete alone does not satisfy such a request, because the data is still stored. Legal requirements vary by jurisdiction, so the exact rules come from the organisation's privacy policy.

Key points

  • Maintain an inventory of personal data
  • Delete, anonymise or retain per table
  • Handle downstream copies
  • Plan for backups and restores
  • Soft delete is not deletion

Common mistakes

  • Deleting the user row and leaving orphans with personal data
  • Forgetting logs and analytics copies

Follow-ups

  • How does anonymisation differ from pseudonymisation?
  • How would you test that deletion is complete?

Tips

  • Separate delete, anonymise and retain