Block rm commands even when you allowed everything else
A PreToolUse hook that stops rm before permission rules are checked, so it beats your allow list.
Published here · 2 sources · Resume reading
Allowed every command so your agent stops asking? Keep one guard: a hook that blocks rm before it runs, even with Bash on your allow list.
Save the hook script
Create .claude/hooks/no-rm.sh in your project [2]:
#!/bin/bash
cmd=$(jq -r '.tool_input.command')
if echo "$cmd" | grep -qE '(^|[^[:alnum:]_-])rm( |$)'; then
echo "Blocked: rm commands are not allowed" >&2
exit 2
fi
exit 0You should see the file in .claude/hooks/.
Make it executable
The hook needs execute permission. You also need jq installed.
chmod +x .claude/hooks/no-rm.shRegister it in settings.json
Add the hook to .claude/settings.json inside the top-level braces [1]:
{
"permissions": { "allow": ["Bash"] },
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/no-rm.sh"
}
]
}
]
}
}Check that it loaded
Type /hooks in Claude Code to see it loaded.
You should see your hook listed under PreToolUse.
How it works
A PreToolUse hook that exits with code 2 stops the tool call before permission rules are evaluated [1]. Its stderr goes back to Claude as the reason. Hook beats allow.
I piped sample hook input into this exact script: rm -rf build, /bin/rm, sudo rm and bash -c 'rm ...' all exit 2; npm test and docker run --rm exit 0.
Warning: This hook also blocks git rm and npm rm. Say yes by hand when you need those. It cannot catch every way to delete a file (find -delete gets past it). For real isolation, use the sandbox [1].
Your agent can now run any shell command except those containing rm.
Send this to the friend who allowed every command so their agent would stop asking.
Keep this guide
Filed under Claude Code, Security
Questions about this guide
Does the hook also block git rm and npm rm?
Yes. The pattern matches any command containing rm as a word. Say yes by hand when you need those.
Can this stop every way to delete a file?
No. Commands like find -delete bypass it. It is a guard, not a security boundary. For real isolation, use the sandbox.
Sources
- Claude Code docs: permissionscode.claude.com
- Claude Code docs: hookscode.claude.com