---
title: "How to roll back what an AI agent changed in your database | Backenly"
description: Schema changes and row changes roll back differently. How to find what an AI agent changed, restore it, and what Backenly can and cannot bring back.
url: https://backenly.com/guides/roll-back-ai-agent-database-changes
markdown_url: https://backenly.com/guides/roll-back-ai-agent-database-changes.md
updated: 2026-11-30
---

# How to roll back what an AI agent changed in your database

A coding agent changes two different things: the schema (tables, columns, policies) and the rows in them. They roll back in different ways, and only schema changes can be rolled back one at a time. This guide covers both, on any PostgreSQL and on Backenly, and what to set up before the agent runs.

Updated November 30, 2026 · Each Backenly fact links the code it describes

## First, find out what changed

Decide which kind of change it was before you roll anything back. Rolling back a schema change does not bring back rows the agent deleted, and restoring rows from a backup does not remove a column the agent added.

| What the agent did | Kind | Where it is recorded |
| --- | --- | --- |
| Created or dropped a table or a column, changed a type, an index or a row-level security policy | Schema | On Backenly, History: who made it, what it did, whether it was verified and whether the state before it can be restored. On plain PostgreSQL, the migration it ran, if it ran one. |
| Changed auth settings, a storage bucket, a function, a webhook, an environment variable, a key, an integration, an allowed origin or an allowed database address | Configuration | On Backenly, History, next to the schema changes. |
| Inserted, updated or deleted rows | Data | No copy of the rows is kept by default, on Backenly or on PostgreSQL. The agent’s tool calls and what they returned are the record: `db_delete` answers with how many rows it deleted. |

Source: [lib/changes/resources/core.ts](https://github.com/backenly/backenly/blob/main/lib/changes/resources/core.ts), [lib/mcp/runtime-db.ts](https://github.com/backenly/backenly/blob/main/lib/mcp/runtime-db.ts)

## Roll back a schema change

On plain PostgreSQL a committed schema change is permanent. Inside an open transaction, `ROLLBACK` discards it ([PostgreSQL: transactions](https://www.postgresql.org/docs/current/tutorial-transactions.html)); after `COMMIT`, it rolls back only if someone wrote the reverse, as a down migration or by hand. A dropped table is gone with its rows unless a backup has it.

On Backenly, every schema change made through Backenly is an entry in the project’s History with a restore point. You can restore the state before one change, or everything back to a point, which restores each later change newest first. A dropped table or column is moved into the change’s vault in the same transaction rather than deleted, so restoring the state before the drop brings it back with its rows.

1. **Ask for the restore.** Your agent calls `deploy { action: "revert_change", changeId }`. In the console, open the change in History and choose Undo this change, or choose Restore to this point to restore everything after it. An agent can ask for a restore; only a person can approve one.
2. **Backenly measures it first.** It checks that the tables the change touched still look as it left them, orders the reverse statements, and measures what the restore would do to data your app wrote since. A restore that cannot work, because a later change touched the same tables or the restore point has expired, is answered at once.
3. **A person approves exactly that.** If restoring would set aside data written since, the request says exactly what, and the person approves that and nothing else.
4. **It runs, then it is verified.** The reverse statements run, and the result is checked against the live database.

- Restore points are kept for 7 days on Free, 30 days on Pro and 90 days on Enterprise. After that, the state before a change cannot be restored from History.
- A History restore never rewinds data your app wrote after the change.
- A change that rewrote values in place, such as a type change, keeps both the old and the new value. A restore puts the old value back only on rows that still hold the changed one, and leaves rows written since as they are.
- Configuration changes restore from History the same way, except that a revoked key never comes back.

Source: [the agent docs for deploy](https://backenly.com/docs/agents/deploy.md), [docs/CHANGE_RECOVERY.md](https://github.com/backenly/backenly/blob/main/docs/CHANGE_RECOVERY.md)

## Roll back rows the agent wrote or deleted

PostgreSQL has no command that reverses a committed row change. There are three ways back, and each has to be in place before the mistake:

- **Point-in-time recovery.** With continuous WAL archiving on, the whole database cluster can be restored to a moment just before the mistake ([PostgreSQL: PITR](https://www.postgresql.org/docs/current/continuous-archiving.html)). Everything written after that moment is lost unless you copy it out first.
- **A backup restored beside production.** Load the last good backup into a separate database ([PostgreSQL: pg_dump](https://www.postgresql.org/docs/current/app-pgdump.html)), then copy back only the rows the agent removed or changed, with `INSERT ... SELECT` or `UPDATE ... FROM`. It takes longer, and it keeps every other write made since the backup.
- **A history table.** A trigger that copies each old row into an audit table before it changes keeps the previous values to copy back ([PostgreSQL: trigger functions](https://www.postgresql.org/docs/current/plpgsql-trigger.html)). It records only what happens after you add it.

On Backenly the agent’s row tools write straight to the table. `db_delete` runs a real `DELETE`, and History keeps no copy of rows. Rows come back from a database snapshot:

- A snapshot is taken every day, and whenever you choose: Database → Snapshots → Take snapshot.
- Snapshots are kept for 7 days, and the two most recent are kept whatever their age.
- Restoring one replaces every table in the project with the snapshot. Rows written since are not kept. The live schema is set aside first, so a restore that fails leaves today’s data in place.
- Only a project admin can restore a snapshot, and never through an AI action.
- A snapshot holds the project’s tables, rows, indexes, constraints and row-level security policies. It does not hold storage files, keys, settings or function code.

### Only the rows the agent touched

A snapshot restore takes the whole project back. To bring back only the rows the agent touched and keep everything written since, you need a copy from before the mistake outside the project:

1. **Export a copy before the task.** An owner or admin downloads a full backup from Postgres on the Connect page: a standard `pg_dump` `.sql` file that restores on any PostgreSQL.
2. **Load it into a scratch database.** On your own machine: `psql "<scratch database URL>" -f <backup>.sql`.
3. **Copy back what was lost.** Query the scratch copy for the rows the agent deleted or changed, then write them back through the REST API or over a read-write connection, which a person arms in the dashboard.

Source: [lib/mcp/runtime-db.ts](https://github.com/backenly/backenly/blob/main/lib/mcp/runtime-db.ts), [lib/services/workspace-backup.ts](https://github.com/backenly/backenly/blob/main/lib/services/workspace-backup.ts), [components/database/DatabaseSnapshots.tsx](https://github.com/backenly/backenly/blob/main/components/database/DatabaseSnapshots.tsx)

## Before the agent runs

- **Take a snapshot first.** A snapshot from a minute before a risky task loses a minute of writes if you restore it; last night’s loses a day. Export a full backup too if you may want only some rows back.
- **Give questions a read-only key.** A read-only key sees 16 of the 23 tools, and a write called anyway is refused with `READ_ONLY_KEY`. The agent cannot upgrade its own key.
- **Let destructive changes wait for you.** Dropping or emptying a table, dropping a column, deleting files and revoking keys do not run on an agent’s say-so: each waits for a person’s approval, including a `DROP TABLE` the agent sends as SQL. `apply_migration` refuses `TRUNCATE` and a `DROP TABLE` inside a larger migration.
- **No table-wide writes.** `db_update` and `db_delete` refuse an empty filter, so there is no update or delete of every row in one call.
- **Prefer a soft delete.** Every table Backenly creates has a `deleted_at` column. An agent told to set it instead of deleting keeps the row, and clearing it brings the row back.
- **Build on a branch.** On Backenly Cloud a preview branch is a copy of the schema, empty unless you copy the data too, where the agent builds and tests before you merge. Branches are in beta.

## What Backenly cannot bring back

- Rows one call at a time. There is no per-row history: rows come back with a whole-project snapshot, or selectively from a backup you exported before.
- Anything older than its window: snapshots after 7 days, restore points after 7 days, 30 days or 90 days, by plan.
- Storage files, keys and settings from a snapshot. Configuration restores from History instead, and a revoked key never comes back.

## Read next

- [Safe database changes with AI agents](https://backenly.com/guides/safe-ai-database-changes): How a schema change is checked, applied and restored, with a production run.
- [Rollback for agent changes](https://backenly.com/features/undo-and-rollback): One History for the schema and configuration, and what a restore keeps.
- [Deploy and rollback](https://backenly.com/docs/deploy): Numbered versions of your backend, and rolling back to an earlier one.
- [Database](https://backenly.com/docs/database): Tables, the REST API, row-level security and how schema changes apply.

---

Source: [How to roll back what an AI agent changed in your database | Backenly](https://backenly.com/guides/roll-back-ai-agent-database-changes). Every page in Markdown: https://backenly.com/index.md. Agent docs: https://backenly.com/llms.txt.
