A small script Claude Code runs every time, before each action. It can block what must never run, and tell Claude why.
2 min to watch3 min to read
Based on a real case: in December 2025, a developer reported that Claude Code ran a cleanup command that deleted most of their Mac's home folder.1,2,3 The command comes from the developer's log, as Simon Willison quoted it. The screens are simplified, the project's name is made up, and the AI's thoughts are written to show the idea.
Without a hook
One extra character, and nothing checked it. A rule in words is a request. Here, the command itself ran unchecked, and rm -rf has no undo. Real case: Claude Code and a developer's home folder, December 2025.
With a hook
Ask nicely for the rest. Put the must-nevers in a hook. One hook blocks deletes outside the project, secrets files, force-pushes and deploys to production, and tells Claude why. Ask Claude to write it, then read it before you turn it on.
Why it matters, what it costs, and what to ask your team.
What happened
In December 2025, a developer asked Claude Code to organize the packages in an old project. To remove a few folders, it wrote one command: rm -rf tests/ patches/ plan/ ~/.1,2
rm -rf deletes, with no undo and no bin. And ~/ means the home folder: the desktop, app data, and the Keychain, where a Mac keeps saved passwords. Most of it was gone.2,3
Reading the log afterwards, Claude found it: "See that ~/ at the end? That's your entire home directory."1
It wasn't the first time. In October 2025, another developer reported on GitHub that Claude Code deleted all the files in their home folder while rebuilding a project.4
I meant to delete three folders. I typed a fourth.
A request vs. a rule
You can give an AI coding agent rules in plain words, like "never delete anything outside this project". It reads them, and usually follows them. Usually.
A sketch to show the idea.
A hook is a small script that Claude Code itself runs at set moments: before a command, after a file edit, and more. Anthropic calls it "deterministic control: certain actions always happen rather than relying on the LLM to choose to run them."5
Today, Claude Code also stops the worst deletes by itself, like one on the home folder, even in its most hands-off mode.7 Hooks are for your own rules: your secrets, your branches, your production.
Guardrails
A hook can also run before each action, and say no. It exits with code 2: the action doesn't run, and the hook's reason goes back to Claude, so it can find another way.5,6 Try it:
Pick something Claude wants to do:
With a hook, the answers come from running the guard in “How to build it”. Without one, this is how the home-folder delete went in 2025. Claude Code now stops that one by itself.
Good first rules:
So the rules I write in words are wishes. The ones in hooks are checks.
Set it up
You don't have to write hooks by hand. Describe the rule in plain words, and Claude writes it.5
One warning, from Anthropic: command hooks "execute shell commands with your full user permissions." Read a hook before you turn it on, even one Claude wrote.6
Under the hood, step by step: build a hook that blocks risky commands, keeps secrets out of reach, and tells Claude why.
Build along
We start with a hook that lets everything through, send it the same six actions each time, and add one rule per step. Press Run it after each step and watch the result change.
Before you trust it
Register the guard in your project's settings. It's the guard.py from the build-along above:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash|Read|Edit|Write",
"hooks": [
{ "type": "command", "command": "python3 \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard.py" }
]
}
]
}
}A hook is one lock, not the only one. Keep Claude Code's own permission questions on, and run risky work in a sandbox.
rm -rf tests/ patches/ plan/ ~/, and "See that ~/ at the end? That's your entire home directory."rm commands, such as one on the home folder, including in bypass-permissions mode.The screens, the project's name, the chat lines and the hooks menu are simplified, and the AI's thoughts are written to show the idea. The command and Claude's line about ~/ come from the developer's log, as Simon Willison quoted it; what was lost comes from the developer's report, as GIGAZINE summed it up. The reports don't say whether the command was approved by hand or ran in a mode that doesn't ask. The "with a hook" replay is made up to show how it could go. The build-along's guard.py is real code, and its outputs come from running it on the six actions shown. It's a starting point, not a complete guard: it won't catch every way to delete a file.