Claude Code Safety

Stop your AI agent from destroying your work — and recover when it does

Hook-based guardrails and recovery runbooks for the irreversible mistakes: deleted databases, wiped files, lost git history. Every hook in it was fired and the exit code read; every incident it cites is cited to a source, and where a piece is missing the book names it.

* * *

Who this is for

You run Claude Code, and you let it do real work — edit files, run shell commands, touch your database. Maybe you run it autonomously, in the background, while you do something else. And somewhere in the back of your mind is the question this guide exists to answer: what stops it from destroying something I can’t get back?

That fear is not paranoia. In 2026 an agent ran drizzle-kit push --force on a production Postgres and wiped more than sixty tables (anthropics/claude-code#27063). Another escalated to prisma db push --force-reset and dropped all eighty-seven (#36183). A widely reported incident saw an AI agent delete a production database during an explicit code freeze, taking data for more than a thousand companies with it — press coverage of a third-party product, and the only incident in this book without a link you can open. The pattern is not exotic. It is a handful of destructive commands, each one a single flag away from being run without a confirmation prompt.

This guide is for the individual operator, not the security team. It assumes you want two things: for the agent to not run the irreversible command in the first place, and — if it already did — a clear, ordered way to get your work back.

What’s inside

For each class of irreversible accident, you get the same three things — and where a class does not get one of them, this book names the exception rather than letting you discover it four chapters later:

  1. The real incident — what actually happened, with the issue or report cited, so you know the failure mode is real and not hypothetical. The git-history chapter is the exception: it is built from the mechanism and the short list of genuinely irreversible commands rather than from one cited case.
  2. The block — the exact PreToolUse hook that stops the command before it executes, ready to paste into settings.json. Every hook in this book was verified by piping the real command JSON through it and reading the exit code. None of them are “documented but not implemented.” /rewind is the exception: slash commands are not tool calls, so no PreToolUse hook sees one and nothing can put a confirmation step inside its menu. §§4.3–4.4 give the recovery order and the settings-level lever, and name the one event where a refusal could still live — marked as unverified, because its reachability is the part I did not test.
  3. The recovery — an ordered runbook for after the fact, and an honest line about what is genuinely unrecoverable, so you don’t waste hours chasing data that is gone.

The accident classes covered: databases (DROP / TRUNCATE / framework --force wipes), files (rm -rf, blind overwrite), git history (reset --hard, clean -fd, bad rebase), and /rewind (the built-in code rollback whose confirm screen starts on the destructive option). A final chapter explains why writing “never delete the database” in CLAUDE.md is not a control point you can check — and what is.

How to use it

If you are about to leave an agent running unattended for the first time, read Chapter 6 (Building your safety net) first, install the combined guard, then come back to the chapter that matches whatever you are most afraid of. Fifteen minutes buys you a recoverable setup.

If you have already been burned, jump straight to the matching chapter’s recovery section. Then install the block so it cannot happen twice.

A note on accuracy and honesty

Every incident in this book is a real, publicly documented event, cited by issue number or report, dated where it matters. The catalogued accidents happened to other people, not to the author, and the citations let you verify each one — no personal “it happened to me” story has been invented to give them weight. (Work has been lost on this setup too, though not to the database commands these chapters are about — and a book that borrowed drama it did not earn would be doing the thing it warns against.) The author’s stake is different: they run Claude Code autonomously and maintain cc-safe-setup, the free, MIT-licensed hook collection these guards come from. The hooks shown here are self-contained versions of the guards in that repository — the maintained files there are more thorough than a block you can paste in one go — and every one was tested against the command it claims to block, and against a safe lookalike it must not block, before it went into this book.

Several things in here are first-hand, and they are marked as such where they appear:

  • Every guard was fired in both directions on 2026-09-03 — the commands that must stop and the ordinary ones that must not. The combined block in Chapter 6 was run against 195 distinct commands, and all 195 behaved as intended — and Appendix B prints every case so you can re-run the set.
  • That test set grew because adversarial review kept finding cases nobody had written down, and most of what it found had been introduced by an earlier round of fixes. Chapter 6 walks through them — including a rewrite that made the rm pattern catch rm -rf "$HOME" while quietly losing rm -rf ~/Documents, and a hole one character wide that survived three rounds because no test case in the table ended in a slash: rm -rf ~/Documents was refused and rm -rf ~/Documents/ was not, which is the form your shell’s tab completion writes for you. They are in the book because the method is the transferable part: a guard is only as good as the commands you have actually fired at it.
  • The cases that still misbehave are printed in the book, with the exact commands, rather than left for you to find — the false stops as well as the things the guard genuinely cannot catch, including the empty-variable accident that is Chapter 2’s own headline story.
  • The 87.3% figure in Chapter 6 — how much of an agent’s real shell traffic arrives as a compound command — was counted from the author’s own session logs that day: 571 transcript files, 755 MB, 43,054 Bash calls. The first count read only half of those, because subagent transcripts are written one directory deeper; it was caught by writing the count a second time, independently, and comparing the population before the percentages. It is one machine’s number, and the book says so.
  • The /rewind behaviour in Chapter 4 was re-checked against the current documentation, the maintainer’s own reproduction, and the shipped 2.1.258 binary — and the first edition of that chapter was wrong. It said the destructive rewind runs with no confirmation. There is a confirmation screen, and it previews what will change. The real gap is narrower and is now described accurately. Correcting it made the chapter shorter and the claim smaller; it also made it true.
  • What the author did not test is separated from what they did, in the same paragraph, wherever the distinction matters.

Where a claim rests on somebody else’s testing rather than the author’s, it says whose. That distinction is the whole point of this section, so it is not blurred anywhere in the book.

The verification runs described in the first person were carried out by the author’s Claude Code agent, on the author’s machine, under the author’s direction — which is also the setup the book is about.

Tooling changes. Flag names and provider features get updated. Where a command’s behavior is version- or provider-specific, the book says so rather than flattening it. Before you rely on any single line in a production setting, confirm it against the current documentation — the book tells you exactly which command and which flag to check.

This is an independent operator’s field guide. It is not affiliated with Anthropic, Vercel/Drizzle, Prisma, or any provider. The free hooks are MIT-licensed; this book is the organized depth on top of them, not the only way to stay safe.