OpenCode plugin that prevents destructive rm -rf commands
npm install opencode-trash-guard

An OpenCode plugin that intercepts dangerous rm -rf commands and rewrites them to use trash for safer file deletion.
rm -rf permanently deletes files with no recovery. trash moves files to the system Trash instead, allowing recovery if needed. This plugin protects you from accidental data loss when using AI coding agents.
Add to your OpenCode config (~/.config/opencode/opencode.json or .opencode/opencode.json):
``json`
{
"plugin": ["opencode-trash-guard"]
}
By default (rewrite mode), when the AI tries to run rm -rf, the plugin:
1. Intercepts the command before execution
2. Rewrites it to use trash insteadtrash
3. Shows feedback so the model learns to use directly
Works regardless of OpenCode's permission settings (even in auto-approve mode).
Set environment variables to customize behavior:
| Variable | Values | Default | Description |
|----------|--------|---------|-------------|
| TRASH_GUARD_MODE | rewrite, deny | rewrite | Rewrite to trash or block entirely |TRASH_GUARD_LEVEL
| | normal, strict | strict | What patterns to catch |TRASH_GUARD_ALLOWLIST
| | comma-separated | - | Patterns to skip (e.g., node_modules,dist) |TRASH_GUARD_COMMAND
| | path | trash | Custom trash binary |
normal:
- rm -rf rm -fr
- rm -r -f
- rm --recursive --force
-
strict (default, catches more):
- All of the above
- rm -r (recursive without force)rm
- , rm .js (wildcards)
The plugin requires a trash command. Most systems have one:
| Platform | Command | Notes |
|----------|---------|-------|
| macOS 15+ | /usr/bin/trash | Built-in ✓ |gio trash
| Linux | | Pre-installed on most desktops |brew install trash
| macOS <15 | | Via Homebrew |
When the AI runs:
`bash`
rm -rf ./old-build
The plugin rewrites it to:
`bash`
trash ./old-build
And shows feedback:
```
⚠️ trash-guard: Rewrote destructive rm command to use trash
💡 Tip: Use 'trash
MIT