Safety hooks against destructive operations

Blocks destructive commands such as deletes before they run, and archives files automatically before they are overwritten.

#Claude Code#hooks#destructive commands#auto backup#guardrails
Destructive-command guard and archive-before-write

WorkflowDestructive-command guard and archive-before-write

Safety Claude Code

Two hooks that run just before Claude Code executes a tool. One blocks destructive commands such as recursive deletes or disk erases unless every target is inside a trash folder or a verified backup is declared. The other saves the previous version of a file, numbered, before any overwrite.

Challenge

  • Writing "never delete" and "back up before overwriting" into the instructions is not enough: over a long task the agent can forget
  • Once data is deleted or a file is overwritten, it is usually too late by the time anyone notices

Approach

  • A pre-Bash hook breaks even compound commands apart, finds dangerous operations, and sends them back with a reason unless the conditions are met
  • A pre-edit hook copies the original file, with a version number, into an archive folder inside the project before letting the edit through

Outcome

  • The baseline rules no longer depend on memory and apply the same way in every project
  • Any file Claude has rewritten can be rolled back to its previous version

The author's global rules include two baselines: delete only when a verified backup exists, and keep the previous version before overwriting. At first they lived in the instruction file, but instructions can be forgotten.

So both were moved into Claude Code hooks, scripts the harness always calls before running a tool. The finer scope, exceptions and history live in a separate skill, and the instruction file keeps only a one-line summary.

The full workflow

TaskWhoInputsOutputs
1. Set the ruleAuthorhuman—Global rules
2. Build + testMain sessionGlobal rulesHooks + tests
3. DeployDeploy scriptsync_toolchain.pyHooks + testsLive hooks
4. Propose callMain sessionRefers to Global rulesTool call
5. Check dangerDelete guarddestructive_guardTool call, Refers to Live hooksVerdict
6. Archive oldArchive hookarchive_before_writeTool call, Refers to Live hooksOld version
7. Run or rerouteMain sessionVerdict, Old versionResult
Destructive-command guard and archive-before-write

7 steps · 1 role · 3 tools · 1 loop · 34 tests · 57 blocks and 12,215 saved versions, 2026-11-03 – 11-18

Turn the baselines you need kept into hooks rather than instructions: they apply the same way every time, remembered or not.

  • Steps 1–3 happen once: the author sets the rule, the main session builds it with tests, and a script deploys it
  • Steps 4–7 happen every time: in any project, two hooks step in just before a tool runs
  • The delete guard can block; the pre-overwrite archive never does
  • The reason for a block goes back to the model, so Claude switches to moving files to the trash or naming a verified backup

Project folders involved (4)

This workflow does not live in one folder: it runs across projects.

claude-infra/src/hooks
The core: source of both hooks, their tests and the deploy script
~/.claude/hooks
Deployed: runs in sessions of every project folder
~/.claude/skills/global-rules
The details, exceptions and history of the baselines
<project>/_archive
Per-project archive of old versions, kept out of Git
Set the rule

Step 1 · Author

Set the ruleglobal rules

Set two baselines: never delete without a verified backup, and keep the old version before overwriting.

  • They used to live in the instruction file, but over long tasks they could be forgotten
  • The instruction file keeps a one-line summary; the details and history moved to a skill
  • Why it was done is kept as a decision record (ADR)
OutputsGlobal rules
Build + test

Step 2 · Main session

Build + testclaude-infra

Build both hooks with tests in the configuration-management project.

  • The delete guard splits compound commands and looks inside sudo and bash -c
  • Anything that cannot be parsed is blocked
  • 34 tests; commands without any risky keyword pass without being parsed
InputsGlobal rules
OutputsHooks + tests
Deploy

Step 3 · Deploy script

Deploydeploy

Deploy to ~/.claude with the sync script and register them in the settings.

  • What actually runs lives in ~/.claude; the repository is its version control
  • Any drift between the two is caught by a test
  • Old files are moved to a trash folder before being overwritten
InputsHooks + tests
OutputsLive hooks
Propose call

Step 4 · Main session

Propose callany project

In any project, Claude calls tools as usual.

  • Claude does not need to remember the hooks
  • Just before each call, the harness always runs them
  • The same rules apply the same way in every project
OutputsTool call
Refers toGlobal rules
Check danger

Step 5 · Delete guard

Check dangercommand check

Block deletes, forced resets, disk erases and the like unless the conditions are met.

  • It passes only if every target is in a trash folder or a real backup is named
  • The session's own temporary folders are exempt
  • Every block is logged with its reason

Common misconceptionSingle-file deletes and overwriting copies are out of scope; it does not stop every deletion

InputsTool call
OutputsVerdict
Refers toLive hooks
Archive old

Step 6 · Archive hook

Archive oldpre-edit copy

Before an existing file is rewritten, save a numbered copy.

  • Copies go into an archive folder inside the project, with a version number and a content hash
  • Identical content is not saved twice; files over 50 MB are skipped
  • Even when it fails, the edit is never blocked

Claude CodeThe archive folder is kept out of Git, so copies never leave the machine

InputsTool call
OutputsOld version
Refers toLive hooks
Run or reroute

Step 7 · Main session

Run or rerouterun tool

If allowed, the tool runs; if blocked, Claude reads the reason and switches to moving or backing up.

  • The reason for a block is returned to the model
  • The usual switch is to move files into a trash folder
  • When a delete is truly needed, a verified backup location is named
InputsVerdict, Old version
OutputsResult

Loops and rework

  • A risky command is blocked → step 4: Read the reason, then move files to the trash or name a verified backup

Key points

Parsing that closes the loopholes. Prefixes like sudo, the bodies of bash -c and eval, and commands chained with pipes or separators are all split apart and checked. Recursive deletes, find with delete, forced git resets and pushes and disk erases are covered, and anything that cannot be parsed is blocked.

Exactly two ways through. A command passes only if every target is inside a trash folder, or it names a backup location that exists and is not empty (the session's own temporary folders are exempt). The reason for a block is returned to the model, so Claude can switch to moving files or making a backup.

Archiving never blocks. The pre-overwrite copy never stops an edit, even when it fails. Identical content is not saved twice, and the archive folder is kept out of Git so backups never leave the machine.