KBCodeKB
Claude CodeUnverified

Fix Claude Code tmpclaude-*-cwd Temp File Pollution in Working Directory on Windows, VS Code, and GitHub Copilot — Cleanup Script Included

Claude Code v2.1.5+ creates `tmpclaude-*-cwd` temporary files directly in the current working directory instead of the system temp directory (`%TEMP%` or `/tmp`). These files accumulate over sessions — users report 119+ files in a single 2.5-hour session — and are never automatically cleaned up. Each file contains a Unix-style path (e.g., `/c/Users/.../project`) and clutters git status output. This is a regression from the original fix in issue #8856, where temp files were correctly placed in `/tmp/claude-*-cwd`. The regression changed both the naming pattern (from `claude-*-cwd` to `tmpclaude-*-cwd`) and the location (from system temp to working directory). The issue primarily affects Windows users (Git Bash, PowerShell, CMD) but also occurs on macOS/Linux and in VS Code when Claude Code is invoked via agent handoff or GitHub Copilot delegation. Anthropic staff (@ashwin-ant) reopened the issue after initial bot-closure, personally merged the fix on February 18, 2026 (commit b757fc9), shipping in v2.1.47+. The latest Claude Code version as of June 12, 2026 is v2.1.175. Users on older versions should upgrade to v2.1.47 or later. For users who cannot upgrade immediately, community cleanup scripts and .gitignore entries provide effective mitigation. 88 reactions and 72 comments confirm widespread impact.

Symptoms

  • Files named `tmpclaude-*-cwd` appear in project working directories after Claude Code sessions
  • Files accumulate over time — users report 119+ files in a single 2.5-hour session and hundreds over weeks
  • Each file contains a Unix-style absolute path (e.g., `/c/Users/username/projects/myapp`) as its content
  • `git status` shows untracked `tmpclaude-*-cwd` files cluttering the working tree
  • Files are never automatically cleaned up by Claude Code — persist across sessions and restarts
  • Also occurs when VS Code delegates to Claude Code via agent handoff, and when GitHub Copilot delegates sessions
  • Occurs even in empty directories with no project files
  • Regression from issue #8856 where temp files were correctly placed in system temp directory

Error signatures

tmpclaude-*-cwd
tmpclaude-
cwd files in working directory

Possible causes

  • In Claude Code v2.1.5, the temporary file creation logic changed from writing to the system temp directory (`/tmp` on Unix, `%TEMP%` on Windows) to writing directly in the current working directory. This is a regression from the fix applied in issue #8856. The root cause is that the file path resolution uses a Unix-style path environment (as evidenced by the Unix paths stored inside the files, e.g., `/c/Users/...`) which fails to resolve the system temp directory on Windows, defaulting to the working directory as a fallback.
  • The file naming pattern also changed from `claude-*-cwd` (in the original #8856 fix) to `tmpclaude-*-cwd`, suggesting a different code path was introduced in v2.1.5 that completely bypasses the system temp directory logic. The new path appears to use `process.cwd()` or equivalent instead of `os.tmpdir()`, causing temp files to land in the project root.
  • On Windows with Git Bash/MSYS2, the path translation layer between Unix-style paths and Windows paths is unreliable. The `os.tmpdir()` call returns a Windows path (e.g., `C:\Users\...\AppData\Local\Temp`) but the file creation code expects a Unix path. The mismatch causes a fallback to the working directory, which is correctly resolved in both path styles.
  • When Claude Code is invoked indirectly (VS Code agent handoff, GitHub Copilot delegation), the working directory context is inherited from the invoking process, which may differ from the expected project root. This causes tmpclaude files to appear in unexpected locations like the VS Code workspace root or the Copilot extension directory.

Solutions

Automated cleanup via cron or pre-commit hook (remove existing tmpclaude files)

risk: lowgithubpublished

Set up automated cleanup that removes tmpclaude-*-cwd files on a schedule (cron/launchd) or before each git commit (pre-commit hook). This community-developed workaround is for users who cannot upgrade immediately. The pre-commit hook approach integrates cleanly with git workflow and requires no external scheduler.

  1. Step 1: Create a cleanup script at a known location: `cat > /usr/local/bin/cleanup-tmpclaude << 'EOF' #!/bin/bash find . -maxdepth 2 -name 'tmpclaude-*-cwd' -delete 2>/dev/null EOF chmod +x /usr/local/bin/cleanup-tmpclaude`
  2. Step 2 (Option A — cron, runs every 30 min): `(crontab -l 2>/dev/null; echo '*/30 * * * * cd /path/to/project && find . -maxdepth 1 -name "tmpclaude-*-cwd" -delete') | crontab -`
  3. Step 2 (Option B — pre-commit hook, runs before each commit): Create `.git/hooks/pre-commit` containing: `#!/bin/bash find "$(git rev-parse --show-toplevel)" -maxdepth 1 -name 'tmpclaude-*-cwd' -delete 2>/dev/null exit 0` then `chmod +x .git/hooks/pre-commit`
  4. Step 3: For macOS users, use launchd instead of cron: create `~/Library/LaunchAgents/com.user.cleanup-tmpclaude.plist` with a 30-minute interval
  5. Step 4: Verify cleanup works: `touch tmpclaude-test-cwd && ls tmpclaude-test-cwd` → file exists, then after cron interval or commit: `ls tmpclaude-test-cwd 2>/dev/null` → expect: 'No such file or directory'

Commands

find . -maxdepth 1 -name 'tmpclaude-*-cwd' -delete 2>/dev/null
touch tmpclaude-test-cwd && ls tmpclaude-*-cwd
ls tmpclaude-*-cwd 2>/dev/null

Config examples

#!/bin/bash
# Save as .git/hooks/pre-commit and chmod +x
# Removes tmpclaude files from repo root before each commit
find "$(git rev-parse --show-toplevel)" -maxdepth 1 -name 'tmpclaude-*-cwd' -delete 2>/dev/null
exit 0

Risks

  • Cron-based cleanup may delete tmp files that an active Claude Code session is using (rare — files are typically written once and left behind)
  • Pre-commit hook adds a small overhead to every commit (typically <100ms)
  • Doesn't address the root cause — upgrade to v2.1.47+ remains the recommended permanent solution

Verification

  • Step 1: `crontab -l 2>/dev/null | grep tmpclaude` → expect: the cron schedule entry with find-delete command
  • Step 2: Create test file: `touch tmpclaude-test-cwd && ls tmpclaude-test-cwd` → expect: 'tmpclaude-test-cwd' listed
  • Step 3: Wait for cron interval or manually run: `find . -maxdepth 1 -name 'tmpclaude-*-cwd' -delete && ls tmpclaude-*-cwd 2>/dev/null` → expect: 'No such file or directory'
  • Step 4 (pre-commit hook): `cat .git/hooks/pre-commit` → expect: script with find-delete and exit 0
0 verified0 failed

Add tmpclaude-*-cwd to .gitignore (immediate git hygiene mitigation)

risk: lowgithubpublished

While waiting to upgrade, prevent tmpclaude files from cluttering git status by adding them to .gitignore. This doesn't stop the files from being created but eliminates the git workflow disruption. Combine with periodic manual cleanup for best results.

  1. Step 1: Add the glob pattern to project .gitignore: `echo 'tmpclaude-*-cwd' >> .gitignore`
  2. Step 2: Remove already-tracked files from git index: `git rm --cached tmpclaude-*-cwd 2>/dev/null`
  3. Step 3: Commit the .gitignore change: `git add .gitignore && git commit -m 'chore: ignore tmpclaude-*-cwd temp files from Claude Code'`
  4. Step 4 (recommended): Add to global gitignore to protect ALL repos: `git config --global core.excludesfile ~/.gitignore_global && echo 'tmpclaude-*-cwd' >> ~/.gitignore_global`
  5. Step 5: Delete existing tmpclaude files: `rm -f tmpclaude-*-cwd`

Commands

echo 'tmpclaude-*-cwd' >> .gitignore
git rm --cached tmpclaude-*-cwd 2>/dev/null
rm -f tmpclaude-*-cwd
git config --global core.excludesfile ~/.gitignore_global
echo 'tmpclaude-*-cwd' >> ~/.gitignore_global

Config examples

# Add to .gitignore or ~/.gitignore_global
tmpclaude-*-cwd

Risks

  • Does not prevent file creation — only hides from git tracking
  • Files continue to accumulate on disk and consume storage (119+ files per session possible)
  • Global gitignore affects all repositories — ensure no legitimate files match the pattern

Verification

  • Step 1: `cat .gitignore | grep tmpclaude` → expect: 'tmpclaude-*-cwd'
  • Step 2: `git status` → expect: tmpclaude files NOT listed in untracked files section
  • Step 3: `ls tmpclaude-*-cwd 2>/dev/null` → files may still exist on disk (this is expected; see Solution 3 for cleanup automation)
  • Step 4 (if using global gitignore): `cat ~/.gitignore_global | grep tmpclaude` → expect: 'tmpclaude-*-cwd'
0 verified0 failed

Upgrade to Claude Code v2.1.47+ (official fix — confirmed by @ashwin-ant)

risk: lowofficialpublished

Anthropic staff (@ashwin-ant) personally reopened and merged the fix on February 18, 2026 (commit b757fc9). The fix shipped in v2.1.47 (published Feb 18, 2026). Upgrade to v2.1.47 or later to resolve the tmpclaude file pollution. The latest version as of June 12, 2026 is v2.1.175.

  1. Step 1: Check current version with `claude --version`
  2. Step 2: Verify current version is affected: if output shows v2.1.5 through v2.1.45, the bug is present
  3. Step 3: Upgrade to latest: `npm install -g @anthropic-ai/claude-code@latest`
  4. Step 4: Verify upgrade: `claude --version` → expect version >= v2.1.47, ideally v2.1.175
  5. Step 5: Clean up existing tmpclaude files: `rm -f tmpclaude-*-cwd` (Git Bash/Linux/macOS) or `del tmpclaude-*-cwd` (CMD/PowerShell)
  6. Step 6: Run a Claude Code session with several tool calls, then exit and verify no new files: `ls tmpclaude-*-cwd 2>/dev/null` → expect: 'No such file or directory'

Commands

claude --version
npm view @anthropic-ai/claude-code version
npm install -g @anthropic-ai/claude-code@latest
claude --version
rm -f tmpclaude-*-cwd
ls tmpclaude-*-cwd 2>/dev/null

Risks

  • Upgrading may introduce other behavioral changes across many versions (v2.1.5 to v2.1.175 spans 130+ releases)
  • npm global install may require sudo/admin privileges on some systems
  • If using npm with nvm, ensure the global install targets the correct Node version

Verification

  • Step 1: `claude --version` → expect: version number >= '2.1.47' (e.g., '2.1.175 (Claude Code)')
  • Step 2: `npm view @anthropic-ai/claude-code version` → expect: '2.1.175' (or later) to confirm latest available
  • Step 3: After upgrade, run Claude Code and execute at least 3 tool calls (e.g., Bash, Read, Grep) in a session
  • Step 4: Exit Claude Code, then: `ls -la tmpclaude-*-cwd 2>/dev/null` → expect: 'No such file or directory' (no new tmpclaude files)
  • Step 5: `git status` → expect: no untracked `tmpclaude-*-cwd` files listed
0 verified0 failed

Agent JSON

Canonical machine-readable representation of this issue:

{
  "issue_id": "1488e0ae-2eae-4b6b-9ad7-c4df275c0808",
  "slug": "fix-claude-code-tmpclaude-cwd-temp-file-pollution-in-working-directory-on-windows-vs-code-and-github-copilot-cleanup-scr-bxg9o6",
  "verification_status": "unverified",
  "canonical_json": "https://codekb.dev/v1/issues/fix-claude-code-tmpclaude-cwd-temp-file-pollution-in-working-directory-on-windows-vs-code-and-github-copilot-cleanup-scr-bxg9o6"
}
← Back to all issuesPowered by CodeKB