---
title: How to make Claude Code send files to the trash bin to prevent accidental deletions
date: "2026-04-27"
author: "Birgit Pauli-Haack"
url: "https://icodeforapurpose.com/how-have-claude-code-send-files-to-the-trash-bin-to-prevent-accidental-deletions/"
categories: ["Site News"]
---

> The full hook code is on GitHub: bph/claude-rm-to-trash

Claude Code is powerful — sometimes a little too powerful. By default, when it runs `rm` it permanently deletes files. One wrong move and your work is gone. I wanted a safety net: instead of deleting, send files to the macOS Trash so I can recover them if needed.

Here’s how I did it with a Claude Code **PreToolUse hook** and Claude’s help

---

## What is a PreToolUse hook?

Claude Code supports hooks — shell scripts that run automatically before (or after) a tool is used. A `PreToolUse` hook can inspect the tool being called, and either allow it, block it, or **rewrite the input** before it runs.

That last part is the key: we can intercept a Bash call containing `rm` and silently swap it for `/usr/bin/trash`.

---

## Step 1: Check that /usr/bin/trash exists

macOS ships with `/usr/bin/trash` — a command-line tool that moves files to the Trash. Test it:

```
echo "test" > /tmp/test.txt /usr/bin/trash /tmp/test.txt
```

```
echo "test" > /tmp/test.txt /usr/bin/trash /tmp/test.txt
```

Check your Trash folder — the file should be there. If `/usr/bin/trash` is missing on your system, install an alternative:

```
brew install trash
```

```
brew install trash
```

And update the path in the script below accordingly.

---

## Step 2: Create the hook script

Create `~/.claude/hooks/rm-to-trash.py`:

	Copy
	
		
			

```
#!/usr/bin/env python3"""Claude Code PreToolUse hook: redirect rm to /usr/bin/trash."""import sysimport jsonimport redef allow(cmd=None):    out = {        "hookSpecificOutput": {            "hookEventName": "PreToolUse",            "permissionDecision": "allow",        }    }    if cmd is not None:        out["hookSpecificOutput"]["updatedInput"] = {"command": cmd}    print(json.dumps(out))def rewrite_rm(command):    """Replace standalone rm [flags] with /usr/bin/trash, stripping flags."""    pattern = r'(?