# Stop Claude Code from deleting files or touching production

> How to keep Claude Code from running rm -rf, force pushing, or touching production: deny rules, a PreToolUse hook that blocks dangerous commands, worktrees, branch protection, and keys the agent never sees.

Updated November 8, 2026 · https://usecoral.app/guides/stop-claude-code-deleting-files

To stop Claude Code from deleting files or touching production, add deny rules for the commands that must never run, a `PreToolUse` hook that blocks `rm -rf`, force pushes and production deploys, and keep production keys off the machine the agent runs on. Then let GitHub branch protection stop anything that reaches `main` without review.

As of November 2026, from Anthropic’s [permissions](https://code.claude.com/docs/en/permissions), [hooks](https://code.claude.com/docs/en/hooks) and [permission modes](https://code.claude.com/docs/en/permission-modes) docs. Part of [Keep coding agents safe](https://usecoral.app/guides/agent-safety).

## The real risk

Agents delete things by accident, mostly while cleaning up:

- **October 2025:** Claude Code on WSL2 ran an `rm -rf` from `/` and deleted every file the user owned ([GitHub issue](https://github.com/anthropics/claude-code/issues/10077)).
- **November 2025:** Claude created a folder literally named `~`, then `rm -rf *` reached the real home folder ([GitHub issue](https://github.com/anthropics/claude-code/issues/12637)).
- **July 2025:** Replit’s agent deleted SaaStr’s production database during a code freeze, after being told not to change anything ([Fortune](https://fortune.com/2025/07/23/ai-coding-tool-replit-wiped-database-called-it-a-catastrophic-failure/)).
- **April 2026:** a Cursor agent on a staging task found a production token and deleted the production database and its backups in one call ([Decrypt](https://decrypt.co/365897/ai-agent-deletes-startup-database-9-seconds-founder-says)).

The last two have the same root cause: the agent could reach production. Rules help, but the strongest fix is that it can’t.

## What Claude Code already does

- **Critical paths ask in every mode.** `rm` on `/`, your home folder, top-level folders or the project’s parents asks even in bypass mode, and no allow rule or hook can approve it. The check covers `rm` and `rmdir` only, and can miss some forms.
- **Auto mode** blocks force pushes, `git reset --hard`, production deploys and migrations, and deleting files that existed before the session. It also misses some, and it allows pushing to `main`.
- **Instructions in chat aren’t rules.** “Don’t push” is honored until the conversation is compacted and the message is gone. For a hard guarantee, Anthropic says: add a deny rule.

## Fix 1: deny rules

Put these in the project’s `.claude/settings.json` so everyone on the repo gets them:

```json
{
  "permissions": {
    "deny": [
      "Read(./.env.production)",
      "Read(./**/.env.production)",
      "Bash(git push --force *)",
      "Bash(git push -f *)",
      "Bash(git push origin main *)"
    ],
    "ask": [
      "Bash(git push *)",
      "Bash(git reset --hard *)",
      "Bash(vercel --prod *)",
      "Bash(supabase db push *)",
      "Bash(terraform apply *)",
      "Bash(kubectl delete *)"
    ]
  }
}
```

Claude Code checks deny first, then ask, then allow, and a deny anywhere wins. Two limits from Anthropic’s docs: rules match the command’s text, so `/bin/rm`, `bash -c '...'` or `git -C . push` get past them. And a `Read` deny stops Claude’s tools and `cat`, but not a Python or Node script reading the file. Rules are a seatbelt; the hook below is a second one.

## Fix 2: a hook that blocks dangerous commands

A `PreToolUse` hook sees every command before it runs and can deny it, even when an allow rule matches. This one, `guard-bash.sh` from the [coral-safety-kit](https://github.com/lior-pesoa/coral-safety-kit), checks each part of a command (it splits on `&&`, `;` and pipes):

- **Denies:** `rm -rf` on anything but rebuildable folders like `node_modules` and `dist`; force pushes; pushes to `main` or `master`, including a bare `git push` while on `main`; production env files; `curl ... | sh`.
- **Asks:** production deploys (`vercel --prod`, `fly deploy`, `terraform apply`, `kubectl delete`, `supabase db push`), `DROP TABLE` and `DELETE` without `WHERE`, `git reset --hard`, `git clean -f`, `npm publish`.

Save it as `.claude/hooks/guard-bash.sh`, run `chmod +x` on it, and add:

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/guard-bash.sh" }
        ]
      }
    ]
  }
}
```

It needs `jq`, which is built into macOS 15 and later. The kit includes a test that runs 50 sample commands through it. Like deny rules, it reads text, so treat it as a second seatbelt, not a wall.

## Fix 3: work in a worktree

Give each agent its own copy of the repo, so a bad delete costs a branch, not your work:

```bash
git worktree add ../myapp-fix-login -b fix-login
cd ../myapp-fix-login && claude
```

Commit before you hand over a big task. Then `git status` shows exactly what changed, and anything deleted is one `git restore` away. More in [Run several Claude Code agents at once](https://usecoral.app/guides/run-multiple-claude-code-agents).

## Fix 4: keep production out of reach

This is the fix that would have stopped the database deletions above:

- **No production keys on dev machines.** Use separate development and staging credentials. If the agent’s machine has no production token, it can’t delete production.
- **Short-lived, narrow tokens** for anything an agent needs, never an owner token.
- **Backups the app’s own token can’t delete.**
- **Branch protection on `main`:** require a pull request and a review, and block force pushes. In GitHub: Settings → Rules → Rulesets.
- **Deploys from CI only,** after merge, not from a laptop.

## Fix 5: the sandbox

`/sandbox` limits Claude’s commands to writing inside the project and temp folders, so an `rm -rf ~` from a command fails at the operating system, not at a text match. It doesn’t cover Claude’s own file tools or MCP servers. See [Claude Code sandbox: what it covers](https://usecoral.app/guides/claude-code-sandbox).

## Safe default to paste

From the [coral-safety-kit](https://github.com/lior-pesoa/coral-safety-kit):

1. Copy `claude/project-settings.json` to `.claude/settings.json` in your repo.
2. Copy `claude/hooks/guard-bash.sh` to `.claude/hooks/` and `chmod +x` it.
3. Merge `claude/user-settings.json` into `~/.claude/settings.json`: sandbox on, bypass mode off, no reading `~/.ssh` or cloud credentials.
4. Turn on branch protection for `main`.

Restart Claude Code, then run `/hooks` to check the hook is listed.

## Where Coral fits

With deny rules and a hook, Claude Code asks more, and an agent that asks while you’re in another app just waits. [Coral](https://usecoral.app/features) shows every permission request in your Mac’s notch with the full command, and you answer with one key. Free on your Mac; answering from your iPhone straight to the agent is [Pro](https://usecoral.app/pricing).

Coming in Coral Terminal: a production fence that asks before anything touches production, and Guard, which shows what a risky command would really do (“Deletes 48 files. 6 aren’t committed.”).

## Questions

### Can Claude Code delete files?

Yes. With your permission, or in auto or bypass mode, it can delete any file your account can. Deleting critical paths like your home folder always asks, but other deletes depend on your mode and rules.

### How do I stop Claude Code from running rm -rf?

Add `"Bash(rm -rf *)"` to `permissions.deny`, and a `PreToolUse` hook that checks every part of each command, since rules match text only. Work in a git worktree and commit first, so anything deleted can be restored.

### Does auto mode stop Claude Code from touching production?

Mostly. It blocks production deploys and migrations by default, but Anthropic measured it missing 17% of real overeager actions. Keep production credentials off the machine and add deny rules.

### Can I tell Claude Code in CLAUDE.md not to push?

You can, and it usually listens, but CLAUDE.md doesn’t change what Claude Code allows. A deny rule is enforced by Claude Code itself.

### What stops Claude Code from deleting my home folder?

Claude Code always asks before `rm` on your home folder, even in bypass mode, and denies it if nobody answers in two minutes. The sandbox stops it at the operating system level too.
