HookPartly tested (needs an account)
Block unsafe rm -rf in Claude Code
recursive-delete-guard
Claude Code hook that blocks rm -rf on unquoted variables, your home folder, a repo root or .git, also inside scripts, even in bypassPermissions mode.
- Works with
- Claude Code
- Good for
- Everyone, Build an app
What it does
Recursive Delete Guard is one Python file that runs as a Claude Code PreToolUse hook before every Bash, Monitor, Write, Edit and MultiEdit call. It blocks rm -r with an unquoted $VAR or $(...), quoted variables whose empty or known value is a protected path, and deletes that target /, your home folder, the current folder or a parent, a git work-tree root, .git, ~/.claude or ~/.ssh. It also blocks find -delete with no name test, xargs rm -r without -0, rsync --delete into those places, and scripts or heredocs that contain any of these.
It follows cd, wrappers such as sudo or timeout, bash -c and eval strings, and shell scripts the command runs. On a block it exits with code 2 and tells Claude why, so Claude can retry with a quoted, guarded path.
Example prompts
- "Run true || rm -rf /tmp/probe-$$" (the README's live check; the rm can never run)
How to install
curl -fsSL https://github.com/mishoko/claude-code-recursive-delete-guard/releases/latest/download/recursive_delete_guard.py -o /tmp/rdg.pypython3 /tmp/rdg.py --installHow to use it
Nothing to type. After install, restart Claude Code. When Claude tries an unsafe recursive delete, the hook blocks it with a reason and Claude can rewrite it with a quoted, guarded path such as rm -rf -- "${D:?}".
Check it works
After install and a restart, ask Claude to run true || rm -rf /tmp/probe-$$. The rm can never run, and you should see Blocked by recursive_delete_guard. You can also run python3 ~/.claude/hooks/recursive_delete_guard.py --self-test.
Common problems
- Nothing is blocked — restart open Claude Code sessions after install; the hook loads at session start — https://github.com/mishoko/claude-code-recursive-delete-guard
- A legitimate delete is blocked — rewrite it as rm -rf -- "${D:?}" with one quoted operand, or run a deliberate protected delete yourself — https://github.com/mishoko/claude-code-recursive-delete-guard
- A block you expected did not happen — check that python3 is on your PATH and the file is in ~/.claude/hooks; a hook that cannot start lets the call run — https://code.claude.com/docs/en/hooks
- Unsure which version is installed — run python3 ~/.claude/hooks/recursive_delete_guard.py --version and rerun the install command to update — https://github.com/mishoko/claude-code-recursive-delete-guard
Other assistants
- Codex and Windows are not supported (README) — https://github.com/mishoko/claude-code-recursive-delete-guard
What to watch out for
- The install line downloads the latest release file from GitHub and runs it. --install copies it to ~/.claude/hooks/recursive_delete_guard.py, runs its self-test (temporary folders only), backs up ~/.claude/settings.json if it exists and adds one PreToolUse entry (matcher Bash|Monitor|Write|Edit|MultiEdit, timeout 10 s)
- Fails closed only inside Python: bad input, an internal error or its own 5 s check limit blocks the call. If the hook cannot start (no python3, file removed) or passes the 10 s timeout the installer sets, Claude Code lets the call run, because the entry has no onFailure: block
- Reads the tool call, the files of shell scripts the command runs (up to 8 levels) and the folders a glob or target points at, to find git work trees; writes nothing while running and makes no network calls
- Never updates itself; rerun the install command to update. Version 1.2.0 (release 2026-11-05)
- macOS and Linux only; not Windows and not Codex. It checks only Bash, Monitor, Write, Edit and MultiEdit calls. It does not catch deletes by other programs (python shutil.rmtree, git clean -fdx, make clean) and does not replace backups
- Claude Code mods: per Claude Code's permissions docs, without managed settings or a Team or Enterprise login, an installed mod that handles tool.check can approve a call that a PreToolUse hook blocked
Licence and author
- Licence
- MIT
- Author
- mishoko
How we tested it
We installed it on an empty test computer on 8 Nov 2026. Partly tested: installed in a clean test system, but its main features need an account or key we did not use.
Not run inside a live Claude Code session, so we did not see Claude receive a block.
Technical details of the test
- Date
- 8 Nov 2026
- Method
- Reviewed by our team against the script at b1213b0 (same sha256 as the v1.2.0 release file), the README and Claude Code's hook docs. In a clean container we ran both install lines: the self-test passed 41 of 41 and settings.json got the entry. We then fed the hook seven tool calls: it blocked the probe command, cd .. && rm -rf proj, a Write of a script with rm -rf $D and bad JSON, and allowed rm -rf -- "${D:?}", rm -rf build and git clean -fdx (a known gap).
Checked again on 8 Nov 2026
Alternatives
- cc-safety-net — a broader guard for risky shell and git commands (also Codex and Gemini CLI); it blocks deletes of your home folder too, but does not read the scripts a command runs. This guard does only recursive deletes, in more depth
FAQ
- Q: Does it work in bypassPermissions mode? A: The README says yes. Claude Code's docs say PreToolUse hooks run before the permission step for every tool, and a hook that exits with code 2 stops the call before permission rules are checked. Claude Code itself also still asks before rm of some critical paths in that mode; this guard adds the unquoted-variable, git-root and inside-a-script cases.
- Q: Does it slow Claude down? A: The README says about 40 ms per tool call, almost all of it Python start-up.
- Q: Can it be fooled? A: Yes, by values only known at run time and by deletes from other programs; the README lists these gaps.
Sources
- github.com/mishoko/claude-code-recursive-delete-guard
- github.com/mishoko/claude-code-recursive-delete-guard/releases/tag/v1.2.0
- code.claude.com/docs/en/permissions
- code.claude.com/docs/en/hooks