Full answer
Steps:
- If the DELETE ran inside an open transaction in your session,
ROLLBACKundoes it immediately. - Otherwise, contain: stop jobs that might cascade or act on missing orders.
- Find the time: the database log or audit trail gives the timestamp of the statement.
- 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. - 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.
- 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