in ai, agents, databases, claude code
On the night of 2 November, a coding agent wiped my development database. Not
once. It ran supabase db reset again and again, each time to get a “clean state” for the
tests it was running.
The database belonged to Mirage, an AI image and video side project of mine. It was a local Supabase stack, so no customers were affected. But it held real data: my account, my generations and my uploads. After a few resets, all of it was gone.
A year earlier I had wiped a day’s work myself with a careless git reset --hard and a force
push, and got all of it back with git reflog. Databases don’t have a reflog.
I am bullish on agents. I use several of them in parallel every day and I don’t plan to stop. That is exactly why this deserved a proper fix rather than a vow to be more careful.
How it happened
Nobody did anything dramatic. The agent was asked to work on a feature and verify it. The project’s test instructions said to reset the database before running the database tests, which is perfectly normal advice on a throwaway CI database. The agent followed it faithfully, on the only database it could see: mine.
Three things lined up:
- “Local” sounded like “disposable”. To the agent, and honestly to the docs I had written, a local dev database was scratch space. To me it was where all my working data lived.
- The instructions were written for a different context. “Reset before testing” is correct in CI and destructive on a developer’s machine. The agent had no way to know the difference.
- Nothing stood between the command and the data. The tool was allowed to run shell commands, and a database reset is just another shell command. Nothing asked for a backup first.
Each of these is a reasonable default on its own. Together they make a very efficient way to lose your data.
Instructions are not guardrails
My first instinct was to add a line to the agent instructions: “never reset the database”. I did add it, but I don’t rely on it. Instructions compete with other instructions, a repo’s own docs can say the opposite, and a long session can drift. If something must never happen, it needs to be enforced by the system, not requested in prose.
Claude Code has hooks for exactly this: scripts that run before a tool call and can block it.
The hook
I added a PreToolUse hook that inspects every shell command before it runs. It is registered
globally in ~/.claude/settings.json, so it protects every project on my machine:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "$HOME/.claude/hooks/block-destructive-db.sh",
"timeout": 10,
"onFailure": "block"
}
]
}
]
}
}
Note onFailure: "block". If the hook itself crashes or times out, the command is blocked, not
allowed. A guardrail that fails open isn’t a guardrail.
The script reads the command, lowercases it and matches it against a list of patterns for anything that destroys data:
cmd=$(jq -r '.tool_input.command // empty')
patterns=(
'supabase([[:space:]].*)?[[:space:]]db[[:space:]]+reset'
'supabase([[:space:]].*)?[[:space:]]stop([[:space:]].*)?--no-backup'
'(^|[^a-z_])dropdb([[:space:]]|$)'
'drop[[:space:]]+(database|schema)'
'truncate[[:space:]]+(table[[:space:]]+)?[a-z_."]+'
'delete[[:space:]]+from[[:space:]]+[a-z_."]+[[:space:]]*(;|"|$)'
'pg_restore([[:space:]].*)?[[:space:]](--clean|-c)([[:space:]]|$)'
'docker([[:space:]].*)?[[:space:]]volume[[:space:]]+(rm|remove|prune)'
# ...plus compose down -v, system prune --volumes, and friends
)
It also reads any SQL file passed with -f or --file, because psql -f cleanup.sql is just
as dangerous as typing the SQL inline. Note the DELETE pattern: it only matches a DELETE
with no WHERE clause, so normal targeted deletes still work.
When something matches, the hook exits with code 2 and explains itself on stderr. Claude Code shows that message to the agent, so the agent doesn’t just fail. It learns why, and what to do instead:
BLOCKED: this command can destroy database data.
- For tests, use a disposable stack with its own project_id and ports.
- If data really must be changed: stop, explain what will be lost,
take a backup, and ask the user.
I can still run any of these commands myself by typing ! <command> in the prompt, because
commands I run directly don’t go through the hook. The agent can propose. Only I can execute.
The rules around it
The hook is the hard stop. Around it I wrote a short set of rules into my global agent instructions, which apply to every project:
- Every database on this machine holds real data. Including local dev stacks. There is no such thing as a scratch database unless it was created for that purpose a minute ago.
- Tests run on a disposable stack. If a repo says to reset before testing, the agent starts
a copy of the project with its own
project_idand ports, runs the tests there and stops it. The dev stack is never touched. - Back up before any write. A single
pg_dumpinto a dated file before any schema or data change, with the path reported back to me. - Don’t work around the hook. No rewording the command, no wrapper scripts, no switching to another tool to get the same effect. If the hook blocks something, the agent stops and asks.
- Check shared state before running agents in parallel. Two agents sharing one database, one Docker volume or one port is how one agent’s “cleanup” becomes another one’s outage.
What it doesn’t solve
Pattern matching is not a sandbox. A determined process could still destroy data through an ORM call, a migration file or a script the hook never sees. The hook catches the common, obvious commands, which happen to be the ones an agent reaches for when it wants a clean slate. The real protection is the combination: the hook, disposable test databases, and backups that make any remaining mistake recoverable.
If I were starting over, I would do it in the reverse order. Backups first, because they cover everything. Disposable test environments second, because they remove the reason to run destructive commands at all. A blocking hook third, for the moments when the first two are forgotten.
Takeaways
- Agents follow instructions literally. Read your repo’s docs as if a very fast, very obedient junior engineer will execute every line on your laptop. Because now one will.
- Enforce, don’t ask. Anything that must never happen belongs in a hook, a permission or a separate environment, not only in a prompt.
- Fail closed. If the safety check breaks, the dangerous action should be what stops.
- Make “local” mean something. Decide which databases are disposable and make the others impossible to confuse with them.
I lost a database to learn this. I hope this post saves you yours.