claude-code - 💡(How to fix) Fix CRITICAL: Claude attempts to delete home directory when cleaning up file named '~' [3 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#49464Fetched 2026-04-17 08:40:26
View on GitHub
Comments
3
Participants
2
Timeline
8
Reactions
1
Author
Timeline (top)
labeled ×5commented ×3

Claude Code attempted to execute rm -f ~/ which would delete the user's entire home directory. This occurred when trying to clean up untracked files that included a literal directory named ~, might also apply to untracked files named ~

Root Cause

Claude misinterpreted directory ./~ (or ~/ in git status) as a shell expansion to the home directory rather than as a literal filename that should be escaped or quoted.

Fix Action

Fix / Workaround

Suggested Mitigations

  1. Add guardrails to prevent rm commands targeting home directory without explicit user confirmation
  2. Improve path handling to properly quote/escape special filenames
  3. Add pattern detection for dangerous commands like rm -rf ~/, rm ~/, etc.
  4. Consider requiring confirmation for any rm command outside the project directory
  5. Train on proper shell escaping for special characters in filenames
RAW_BUFFERClick to expand / collapse

Summary

Claude Code attempted to execute rm -f ~/ which would delete the user's entire home directory. This occurred when trying to clean up untracked files that included a literal directory named ~, might also apply to untracked files named ~

Context

  • Working directory had untracked files including: .blah.swp, .gitignore.swp, and a directory literally named ~
  • Git status showed these as untracked: ~~ ~/ (the ~ is the status, ~/ is the directory)
  • User asked to fix PR checks which included a "boilerplate integrity" failure due to uncommitted changes
  • Claude attempted to clean up swap files and other unnecessary untracked files

What Happened

  1. Claude successfully removed swap files: rm -f .blah.swp .gitignore.swp
  2. Claude then attempted: rm -f ~/ intending to remove the local directory named ./~
  3. This command would actually delete the USER'S HOME DIRECTORY
  4. User blocked the command and reported the issue

Root Cause

Claude misinterpreted directory ./~ (or ~/ in git status) as a shell expansion to the home directory rather than as a literal filename that should be escaped or quoted.

Expected Behavior

When handling a directory or files literally named ~, Claude should:

  • Use rm -f './~' or rm -f '~' with proper quoting
  • Or use rm -f -- ~ to prevent interpretation
  • Never execute rm -f ~/ without explicit user confirmation for home directory deletion

Impact

CRITICAL SEVERITY: This could result in catastrophic data loss - deletion of user's entire home directory including all personal files, configurations, and potentially unrecoverable work.

Suggested Mitigations

  1. Add guardrails to prevent rm commands targeting home directory without explicit user confirmation
  2. Improve path handling to properly quote/escape special filenames
  3. Add pattern detection for dangerous commands like rm -rf ~/, rm ~/, etc.
  4. Consider requiring confirmation for any rm command outside the project directory
  5. Train on proper shell escaping for special characters in filenames

Reproduction

  1. Create a file named ~ in a git repository: touch '~'
  2. Add other untracked files like .file.swp
  3. Ask Claude to clean up untracked swap files and unnecessary files
  4. Observe Claude attempts rm -f ~/

Environment

  • Platform: darwin (macOS)
  • Shell: zsh
  • Model: Sonnet 4.5 (us.anthropic.claude-sonnet-4-5-20250929-v1:0)
  • Git repository with untracked files including literal ~ filename

extent analysis

TL;DR

To prevent accidental deletion of the user's home directory, Claude should properly quote or escape special filenames like ~ when executing rm commands.

Guidance

  • When handling files or directories literally named ~, use rm -f './~' or rm -f '~' with proper quoting to prevent shell expansion.
  • Use rm -f -- ~ to prevent interpretation of ~ as the home directory.
  • Implement guardrails to require explicit user confirmation for rm commands that could potentially target the home directory.
  • Improve path handling to properly quote or escape special filenames to avoid similar issues in the future.

Example

# Safe removal of a file named ~
rm -f './~'

# Alternative safe removal using --
rm -f -- '~'

Notes

The issue is specific to the handling of special filenames like ~ and the potential for catastrophic data loss if not properly addressed. The suggested mitigations aim to prevent such incidents by improving path handling and requiring confirmation for potentially dangerous commands.

Recommendation

Apply workaround: Implement proper quoting or escaping of special filenames like ~ when executing rm commands to prevent accidental deletion of the user's home directory. This is a critical severity issue that requires immediate attention to prevent data loss.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix CRITICAL: Claude attempts to delete home directory when cleaning up file named '~' [3 comments, 2 participants]