> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Replicas Cant Save You From Deletes

> Explains why replicas can't recover accidental DELETEs and recommends backups with WAL/binlog point in time recovery and tested restore procedures

Let's walk through a common, tricky database interview (and operational) question.

You have one primary database and two replicas. An administrator runs a DELETE on the primary and accidentally removes half the rows. Can you restore those deleted rows from the replicas?

Example accidental command:

```sql theme={null}
db=# DELETE FROM users WHERE last_login < '2023-01-01';
DELETE 10000000
```

The instinctive answer for many is “yes — replicas have a full copy,” but that is incorrect.

Why replicas usually can't recover accidental deletes

* Replicas are designed to keep a copy of the primary as closely synchronized as possible. They receive changes from the primary (commonly via WAL/transaction streaming) and apply them automatically.
* When the primary deletes 10 million rows, the replicas receive and apply that same DELETE almost immediately. Replication applies the primary’s changes deterministically and does not evaluate whether an operation was intended or accidental.
* By the time someone notices the mistake, replicas will most likely have the same missing data.

There are only a few realistic exceptions:

* A replica that is significantly behind (replication lag) and has not yet received the DELETE.
* Replication that was paused or stopped before the DELETE arrived.
  These are edge cases and unreliable to depend on as a recovery strategy.

Replication protects availability and failover — it helps keep your service online if the primary fails. It is not a substitute for backups when protecting against human error or logical corruption.

What you need instead: backups and WAL/binlog-based recovery
To protect against accidental deletes and other logical mistakes, adopt a proper backup and recovery strategy:

* Regular, off-host backups/snapshots stored in durable object storage (for example, Amazon S3) or another immutable/offsite location.
* WAL (PostgreSQL) or binary log (MySQL) archiving to enable point-in-time recovery (PITR).
* A documented and tested restore procedure: restore the backup, then replay WAL/binlogs up to just before the bad operation.

Comparison: replication vs. backups

| Mechanism | Protects against | Typical use case | Recovery limitations |
| - | - | - | - |
| Replica(s) | Node failure, read scaling, HA | Fast failover, read scaling | Replicas replicate bad writes/deletes; not useful for logical errors |
| Backups + WAL/binlogs | Accidental deletes, logical corruption, long-term retention | PITR, full restore, long-term archival | Requires storage and tested restore process |

High-level recovery workflow (recommended)

1. Restore the most recent backup/snapshot to an isolated instance.
2. Replay WAL/binlogs up to the timestamp or LSN just before the accidental DELETE (PITR).
3. Validate the recovered data, then:
   * Bring the recovered instance into production, or
   * Extract only the missing rows and re-insert them into the live database after verification.

Example: Postgres PITR (conceptual)

* Restore base backup.
* Set `recovery_target_time` or `recovery_target_lsn` in `recovery.conf` (or use `pg_restore`/`pg_basebackup` workflows).
* Start recovery and stop at the target time/LSN, then validate.

Practical tips

* Keep frequent backups and retain them for a period that matches your business’s RPO/RTO requirements.
* Archive WAL/transaction logs continuously and ensure the archive is tested and restorable.
* Regularly run simulated restores (drills) to confirm you can recover within your desired window.
* Monitor replication lag — while you shouldn’t rely on lag for recovery, knowing replica lag can help in emergency triage.

Backups should be regular, stored off the live system, and you should routinely test restores. Without backups and WAL/transaction log archiving, replicas cannot save you from accidental deletes.

Further reading and references

* [Point-in-time recovery (PITR) for PostgreSQL](https://www.postgresql.org/docs/current/continuous-archiving.html)
* [Database backup strategies and best practices](https://aws.amazon.com/backup/)

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/1ee75c71-d566-42df-8cc2-7aa8e79b0f04/lesson/9e6e0ed7-4149-4968-9548-2f7c127cee50)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.