# 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.

Source: https://fullbytedeveloper.com/blog/stop-rm
Published here: 2026-11-10

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]:

```bash title=".claude/hooks/no-rm.sh"
#!/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 0
```

You should see the file in `.claude/hooks/`.

## Make it executable

The hook needs execute permission. You also need `jq` installed.

```bash
chmod +x .claude/hooks/no-rm.sh
```

## Register it in settings.json

Add the hook to `.claude/settings.json` inside the top-level braces [1]:

```json title=".claude/settings.json"
{
  "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.

## Sources

1. [Claude Code docs: permissions](https://code.claude.com/docs/en/permissions)
2. [Claude Code docs: hooks](https://code.claude.com/docs/en/hooks)
