← Back to SQL, page 22

sql-0434 · Hard senior

Someone ran DELETE FROM orders without a WHERE clause on production. What do you do?

Stop further damage first (pause jobs and writes that depend on the table), then restore the deleted rows from a point-in-time recovery to a separate server, just before the DELETE, and copy them back. If the session is still open and uncommitted, ROLLBACK. Afterwards, add safeguards such as reviewed scripts, least-privilege roles and statement guards.

Full answer

Steps:

  1. If the DELETE ran inside an open transaction in your session, ROLLBACK undoes it immediately.
  2. Otherwise, contain: stop jobs that might cascade or act on missing orders.
  3. Find the time: the database log or audit trail gives the timestamp of the statement.
  4. Restore a copy: recover a backup plus WAL to a new server, stopping just before that moment (recovery_target_time). Do not restore over production, which would lose later legitimate writes.
  5. Copy back: export the orders table from the restored copy and insert the missing rows, taking care with rows changed or added since, and with dependent tables if ON DELETE CASCADE removed children.
  6. Verify counts and totals, then resume.

Prevention:

  • Application and human roles without DELETE on critical tables, or deletes allowed only through functions.
  • Run manual changes inside BEGIN, check the row count, then commit.
  • Tools that refuse UPDATE or DELETE without WHERE (MySQL's safe-updates mode; review policies elsewhere).
  • Soft delete or audit logs for critical records.

Replicas do not help here, because the DELETE replicated to them within seconds.

Key points

  • Rollback if still in the transaction
  • Contain before fixing
  • Restore to a separate server at a point in time
  • Copy missing rows back carefully
  • Add guardrails afterwards

Common mistakes

  • Restoring the whole database over production
  • Assuming replicas keep the deleted rows

Follow-ups

  • What if cascades deleted order items too?
  • How would you rehearse this recovery?

Tips

  • Describe the restore-to-side approach