An AI agent deleted my database. What now?
Updated · DBShifts · commands tested on real PostgreSQL, MySQL and MongoDB servers
Short answer
Stop the agent and lock the database account it used before anything else, then stop all writes so nothing overwrites what can still be recovered. Restore a copy from a point in time just before the damage — your provider's point-in-time recovery, or your last backup plus the binary log — copy the lost tables back into the live database, and compare row counts before you call it fixed.
Key facts
- Point-in-time recovery only reaches back as far as the retention window your plan keeps. Check that window first: it is the deadline for everything below.
- Restore into a separate database, never over the live one. The live one holds every good write made since the incident; the restore is where you copy the lost rows from.
- A dropped PostgreSQL table is gone from the live database. Getting it back takes a backup, WAL archiving or your provider's point-in-time recovery.
- MySQL 8 keeps a binary log by default, so the changes since your last backup can be replayed up to the statement just before the DROP.
- Agents connect with whatever credentials your project gives them, usually DATABASE_URL in .env. Lock or rotate that account even if you think the agent has stopped.
- In July 2025 an AI agent on Replit deleted a company's production database during a code freeze. It is the incident most people know, not a rare one.
1. Stop the agent and cut its access
Cancel the agent's run and close the terminal it was using. Then make sure it cannot reconnect: lock the database account it connected with and end the sessions that account still has open. If the agent used your app's own account (one DATABASE_URL for everything is common), change that account's password instead and update the app after.
ALTER ROLE app_agent NOLOGIN;
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename = 'app_agent';ALTER USER 'app_agent'@'%' ACCOUNT LOCK;
SELECT id, user, host, command FROM information_schema.processlist WHERE user = 'app_agent';
-- then, for each id it lists:
-- KILL 1234;2. Stop writes, and write down the time
Every write after the incident can overwrite the space the lost data sat in, and makes the restore harder to line up. Put the app in maintenance mode, or make the database refuse writes. Then note the exact time, in UTC, just before the damage: the agent's transcript and your database logs usually have it to the second.
ALTER DATABASE app SET default_transaction_read_only = on;
-- when you are done:
-- ALTER DATABASE app RESET default_transaction_read_only;SET GLOBAL super_read_only = ON;
-- when you are done (super_read_only leaves read_only on, so undo both):
-- SET GLOBAL super_read_only = OFF;
-- SET GLOBAL read_only = OFF;3. Find exactly what was lost
Do not trust the agent's summary of what it did; read the database. In PostgreSQL, list the tables and their approximate size and compare with what you expect. In MySQL, the binary log holds the statement itself and its position, which is what you replay up to in step 4.
SELECT relname AS table_name, n_live_tup AS rows_estimate
FROM pg_stat_user_tables
ORDER BY relname;SHOW BINARY LOGS;
SHOW BINLOG EVENTS IN 'binlog.000012';4. Restore a copy from just before the damage
On a managed database, use point-in-time recovery to a time just before the one you wrote down. Some providers restore into a new instance or branch (AWS RDS, Neon); others restore the project in place, which overwrites everything written since — read your provider's restore page first, and take a backup of the live database before you start.
Self-hosted MySQL: restore your last backup into a new database, then replay the binary log from the backup's position up to the destructive statement. The backup command below records its own starting position in the dump; SHOW BINLOG EVENTS (step 3) gives the position to stop at — use the Gtid or Anonymous_Gtid event just before the DROP, so the replay ends between transactions. mysqlbinlog ships with the MySQL client tools, not with the official MySQL Docker image.
If all you have is a pg_dump archive, you can restore one table from it into the live database — knowing it will only have the rows that existed when the backup ran.
mysqldump --single-transaction --routines --triggers --events --source-data=2 app > app.sql
grep -m1 'CHANGE REPLICATION SOURCE TO' app.sqlmysql -e "CREATE DATABASE app_restore"
mysql app_restore < app.sql
mysqlbinlog --start-position=1574 --stop-position=2391 \
--rewrite-db='app->app_restore' --database=app_restore \
/var/lib/mysql/binlog.000012 | mysql app_restorepg_restore --dbname=app --table=orders app.dump5. Copy back only what was lost
With a restored copy running, copy the lost tables from it into the live database. Copy only what the agent destroyed: the rest of the live database has newer data than the copy.
pg_dump --table=orders app_restore | psql appmysqldump app_restore orders | mysql app6. Check before you switch back
Count the rows in every table on both sides, and compare the tables that matter by content, not only by count — a table can have the right number of rows and the wrong rows. Then lift the write freeze and bring the app back.
SELECT table_name,
(xpath('/row/n/text()',
query_to_xml(format('SELECT count(*) AS n FROM %I.%I', table_schema, table_name),
false, true, '')))[1]::text::bigint AS row_count
FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
ORDER BY table_name;7. Make it impossible next time
Give agents a database they are allowed to break, a read-only account if they must see production, command approval in your AI tool, and a backup you have actually restored. Each of those has its own guide below.
Common mistakes
Restoring over the live database
It replaces every good write since the incident with an older copy. Restore beside it and copy back only what was lost.
Asking the agent to fix it
The agent that caused the damage still has the access and the same plan. Take its access away first; recover by hand.
Believing the agent's account of what it did
Agents report confidently and sometimes wrongly. The binary log, the database log and the restored copy are the record.
Not rotating the credentials
The password is in the agent's context and maybe in a transcript. Lock the account or change the password even after the agent stops.
Waiting past the retention window
Point-in-time recovery has a limit measured in days. Start the restore before you start the post-mortem.
Checking only row counts
Counts can match after a partial or out-of-order replay. Compare content (checksums) on the tables that matter.
How DBShifts helps
- Airlock gives agents a token instead of a database password: read-only by default, sensitive columns masked, every request logged, and a kill switch that stops every agent on a gateway at once.
- Writes from agents are proposals a person approves, with undo and version history; no API key or AI tool can change those rules (the human lock).
- After a restore, DBShifts validation compares the restored and live tables by row counts and checksums.
Questions people ask
Can an AI coding agent really delete my production database?
Yes, if it can reach the database with an account allowed to drop or delete, and it can run commands. Coding agents run terminal commands and read your project's .env file; if production credentials are there, so is production.
Can I recover a dropped PostgreSQL table without a backup?
Not from the live database. You need a backup, archived WAL, or your provider's point-in-time recovery. Check the provider's retention window immediately: it is usually days, not weeks.
How do I find the exact statement the agent ran?
Read the agent's transcript, then confirm it in the database: MySQL's binary log (SHOW BINLOG EVENTS) has the statement and its position; PostgreSQL logs it if log_statement includes DDL, and pg_stat_statements keeps normalised queries if it is enabled.
Will point-in-time recovery lose the writes made after the incident?
If you restore in place, yes. Restore into a new database and copy the lost tables back into the live one, and you keep both.
Never hand an agent the database password again
Scoped tokens, read-only by default, every request logged, one switch to stop them all. Free to start.
7 guides in this series. See the checklist.