opencode-trash-guard
v1.0.3
Published
OpenCode plugin that prevents destructive rm -rf commands
Downloads
389
Readme
opencode-trash-guard
An OpenCode plugin that intercepts dangerous rm -rf commands and rewrites them to use trash for safer file deletion.
Why?
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.
Installation
Add to your OpenCode config (~/.config/opencode/opencode.json or .opencode/opencode.json):
{
"plugin": ["opencode-trash-guard"]
}How it works
By default (rewrite mode), when the AI tries to run rm -rf, the plugin:
- Intercepts the command before execution
- Rewrites it to use
trashinstead - Shows feedback so the model learns to use
trashdirectly
Works regardless of OpenCode's permission settings (even in auto-approve mode).
Configuration
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 |
Detection levels
normal:
rm -rf <path>rm -fr <path>rm -r -f <path>rm --recursive --force <path>
strict (default, catches more):
- All of the above
rm -r <path>(recursive without force)rm *,rm *.js(wildcards)
Trash Command
The plugin requires a trash command. Most systems have one:
| Platform | Command | Notes |
|----------|---------|-------|
| macOS 15+ | /usr/bin/trash | Built-in ✓ |
| Linux | gio trash | Pre-installed on most desktops |
| macOS <15 | brew install trash | Via Homebrew |
Example
When the AI runs:
rm -rf ./old-buildThe plugin rewrites it to:
trash ./old-buildAnd shows feedback:
⚠️ trash-guard: Rewrote destructive rm command to use trash
💡 Tip: Use 'trash <path>' directly for safer deletionLicense
MIT
