Memory leak: Missing cleanup for /tmp/claude-*-cwd working directory tracking files
Bug Description
Claude Code creates temporary files to track working directory changes across Bash command executions but never deletes them, causing accumulation of /tmp/claude-*-cwd files.
Environment
- Claude Code Version: 2.0.1
- OS: Linux (WSL2)
- Node Version: (System default)
The Problem
Every Bash tool invocation creates a temporary file at /tmp/claude-{random-4-hex}-cwd to track the working directory after command execution. These files are never cleaned up, leading to accumulation.
Evidence
- Observed: 174 files accumulated in one day of usage
- Pattern: Each file is 22 bytes containing the working directory path
- Debug log: Shows 2,018 Bash invocations over 4 days
- Only cleanup: systemd-tmpfiles-clean removes them daily at 07:15
Root Cause Analysis
Location in Code
File: /usr/local/lib/node_modules/@anthropic-ai/claude-code/cli.js
The issue is in the lq6 function (around line 1520 in the minified code):
// Current implementation (BUG - no cleanup):
P.result.then(async(k)=>{
if(k&&!Y&&!k.backgroundTaskId)try{
j$(bq6(K,{encoding:"utf8"}).trim(),M) // Reads file but never deletes it
}catch{
B1("tengu_shell_set_cwd",{success:!1})
}
})
The Mechanism
- Claude Code generates a random temp file path:
let K = \${V}/claude-${F}-cwd\`` - Appends
pwd -P >| ${K}to every Bash command to capture final directory - Reads the file with
bq6(K,{encoding:"utf8"})to update internal cwd - Missing step: Never calls
unlinkSync(K)to delete the file
The Fix
Add cleanup immediately after reading the file:
// Fixed implementation:
P.result.then(async(k)=>{
if(k&&!Y&&!k.backgroundTaskId)try{
let cwdContent=bq6(K,{encoding:"utf8"}).trim();
try{C1().unlinkSync(K)}catch{}; // <- Add this line
j$(cwdContent,M)
}catch{
B1("tengu_shell_set_cwd",{success:!1})
}
})
Verification
I applied this fix locally and confirmed:
- Files are now created and immediately deleted after use
- Working directory tracking continues to function correctly
- No accumulation even after running 15+ commands in succession
Impact
- Resource leak: Accumulates filesystem entries (500+ files/day for heavy users)
- Privacy concern: Working directory paths persist in /tmp
- Disk usage: While minimal (22 bytes each), it's unnecessary accumulation
Reproduction Steps
- Run any Bash command:
ls - Check temp files:
ls /tmp/claude-*-cwd - Observe new file created but not deleted
- Repeat and watch accumulation
Why This Design Exists
The temp file approach is actually clever and necessary:
- Tracks
cdcommands across subprocess isolation - Doesn't pollute stdout with pwd output
- Works reliably across all shell types
- The implementation is 95% correct, just missing the cleanup step
Recommendation
This is a simple one-line fix that should be included in the next Claude Code release. The temp file mechanism itself is sound and necessary for stateful directory navigation in a stateless subprocess model.
Additional Notes
- The fix has been tested and verified locally
- No side effects observed
- This affects all Claude Code users on Unix-like systems
Showing cached comments. Read the full discussion on GitHub ↗
106 Comments
Additional Analysis & Impact Assessment
Quantitative Impact Data
After monitoring for 24 hours, here's the accumulation rate:
Why This Matters
Reproduction Script
Suggested Priority
Given the silent nature of this bug (no errors, just accumulation), I'd suggest Medium Priority with the following reasoning:
Testing the Fix
The fix has been verified locally by modifying
/usr/local/lib/node_modules/@anthropic-ai/claude-code/cli.js:Happy to provide any additional information or testing assistance needed!
Have we resolved this issue?
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
The issue still persists and is really annoying. After a few days the amount of claude-*-cwd files reaches thousands which really slows things down.
<img width="245" height="607" alt="Image" src="https://github.com/user-attachments/assets/fb719adb-db38-478c-bbb6-5e8cc10f1516" />
Issue: Temporary working directories not being cleaned up
I'm still experiencing this issue. As shown in the screenshot, multiple tmpclaude-XXXX-cwd directories are accumulating and not being properly cleaned up after sessions end.
Environment:
Multiple temporary directories with pattern tmpclaude-*-cwd persist after use
Directories observed: tmpclaude-02a6-cwd, tmpclaude-6f46-cwd, tmpclaude-7b7a-cwd, tmpclaude-67aa-cwd, tmpclaude-144b-cwd, tmpclaude-191b-cwd, tmpclaude-685d-cwd, tmpclaude-1687-cwd, tmpclaude-2601-cwd, tmpclaude-cb02-cwd, tmpclaude-d549-cwd, tmpclaude-e2ac-cwd, tmpclaude-f0c9-cwd, tmpclaude-f096-cwd, tmpclaude-fb05-cwd, tmpclaude-fe76-cwd, and more...
Expected behavior:
Temporary working directories should be automatically removed when the session ends or the tool exits.
Actual behavior:
Temporary directories persist indefinitely, requiring manual cleanup.
This is still actively occurring. Would appreciate a fix for proper cleanup of these temporary directories.
as the bug is tagged with linux: This is also an issue in windows:
<img width="629" height="543" alt="Image" src="https://github.com/user-attachments/assets/d6747842-9352-4265-8ea6-80f0eb80cdfa" />
<img width="1056" height="535" alt="Image" src="https://github.com/user-attachments/assets/1124fded-24d5-41bb-a98c-e31342298ac8" /> Still persists in windows. Latest claude code 2.1.5
Now I have files like this created right in the working repository at the root...
<img width="640" height="505" alt="Image" src="https://github.com/user-attachments/assets/0580aac0-34da-404b-97d6-deb53c7c98bd" />
Same issue:
<img width="281" height="756" alt="Image" src="https://github.com/user-attachments/assets/1bced9ab-4ef0-44d9-b204-25b56f912c81" />
Same issue
+1 same issue
+1 same issue
+1
+1, this is spewing these files out all over my Unity project...
Anthopic - you have no moat. Just open-source Claude Code already, there is zero reason not to.
Opus 4.5 as "the best model" lasted for a few weeks. Now it really makes little difference if a prompt is ran on Claude Code with Opus 4.5 or on Codex/OpenCode with GPT-5.2.
Even GLM-4.7, albeit slow and less capable is very usable if you just give it a bunch of work (with a proper validation harness) and come back tomorrow.
As for CC pretty much every day I have a situation where one CC instance is eating 100% CPU just sitting there waiting for text input. And I have to run a
while :; do chmod -R 777 /tmp/claude*; sleep 2; donesimply because if you run two CC instances on different users they will fight over the permissions to the/tmp/claude*directories.AI companies are like utility - you don't care where your electricity is coming from, only that it's 220V. And I'm certain that MiniMax 3, GLM-5, GPT-6 and Grok 5 will be better than Opus 4.6.
Again - you have no moat. Stop trying to build a "walled garden" and let people use a harness that's NOT broken. Or just open-source it so we can fix it by ourselves.
Moats aren't a thing when switching agents is literally an
npm installaway. And CC is the only major closed-source agent.+1
Affects Version / Steps to reproduce
install.psscript: https://code.claude.com/docs/en/setup#standard-installationWorkaround
tmpclaude*to.gitignoreclaude.md:``
tmpclaudeClean up any files that match the GLOB pattern
after you are done working usingrm ./tmpclaude.
``same issue on Windows and Claude Code 2.1.5
same issue on Windows and Claude Code 2.1.5
I have a workaround using a PowerShell script that runs automatically via Claude Code's
Stophook.Work with Claude Code 2.1.5 on Windows
1. Create cleanup script at
C:\Users\<YourUsername>\.claude\scripts\cleanup-claude-temp.ps1:2. Add Stop hook to your settings (
C:\Users\<YourUsername>\.claude\settings.json):3. Restart all terminals and editors. Problem solve
Same issue on Windows and Claude Code 2.1.5
Same issue on windows 2.1.5
Same issue on Windows and Claude Code 2.1.5
Same issue on windows 2.1.5
I get an error with this due to new format - had to switch settings.json to be:
same issue, very annoying
+1 windows, at least update the changelog when you push the version please
+1 windows
+1
+1 windows
+1 windows - only started for me today
+1
Duplicate of #17777
+1
+1
For the love of Claude. Stop with the +1 and "me too!".
We know. Just subscribe to the issue and wait for actual meaningful updates.
This seems like a good idea. How to handle the case where multiple claudes are running in the same folder?
same issue
Same issue. This is ridiculous
<img width="565" height="781" alt="Image" src="https://github.com/user-attachments/assets/706ab6c1-db6f-41dd-99fd-43ddb4e378ad" />
+1 as well
<img width="277" height="269" alt="Image" src="https://github.com/user-attachments/assets/7ab4e0cb-382a-49aa-b688-399da40d4ef5" />
<img width="525" height="599" alt="Image" src="https://github.com/user-attachments/assets/94d843cb-4e1d-463a-a0b5-167b2a0dfc0e" />
nice work :-D
this happening since last 2 days after i ddi claude update
Also experiencing this issue. Over SSH in a vscode remote session.
Same here, that is funny
<img width="314" height="365" alt="Image" src="https://github.com/user-attachments/assets/8f09c4f3-f7b1-46ee-889e-e9ed8bbe9760" />
Platform: win64
Shell: PowerShell
Third-party endpoint (blue whale)
Staying up to date
+1 v2.1.5 windows
also this version same issue
2.1.6 (Claude Code)
+1 2.1.6 (Claude Code) Windows
<img width="738" height="480" alt="Image" src="https://github.com/user-attachments/assets/e178683c-3767-4839-b411-9613a0b9e701" />
+1 v2.1.5 windows
Linux users are fine, but on Windows it creates a bunch of temporary files and
nulfiles directly in the current project’s root directory.Linux 用户还好呢,windows 用户直接在当前项目根目录生成一堆临时文件和nul文件;
<img width="685" height="1163" alt="Image" src="https://github.com/user-attachments/assets/e8d549a7-b663-4111-9b67-58711a94ef4f" />
Version 2.1.6, the issue indeed still persists.
<img width="568" height="541" alt="Image" src="https://github.com/user-attachments/assets/d911992b-d9da-4185-9941-88fc58c873e8" />
v2.1.5
Same here. really annoying
<img width="376" height="914" alt="Image" src="https://github.com/user-attachments/assets/4ef67bcd-e44d-4ff6-b01d-c0118e022df1" />
Please fix this. crazy annoying
Yeah, this is crazy! These files are now not showing in the tmp directory, but IN THE REPOSITORY, and not always in the same directory but scattered around!
Fix this, please, it's unmanageable!
It seems the files get generated when claude executes bash commands on windows
Please fix this... it's crazy annoying
Git Bash right?
Yes basically if I tell it to create a hello world file it wont generate any of the tmp files. When I tell it to delete it and it executes Bash tool it generates the tmp file.
Do you have a rule to deny rm commands? Or maybe some git bash issue deleting files with rm command or preventing deleting, I don't know...
No rules like that, I allow deletes. Its just an example. Any bash tool call will generate the tmp files.
!Image
+1
+1
+1
+1
+1
I got this also happening in a tanstack start project and even more so in my dotnet project
+1
+1
started happening for me yesterday/today after latest update. Windows 11 25H2! Running on Windows Terminal
upvote
+1
+1
+1
+1
+1
Claude... you cooked!
+1
<img width="271" height="642" alt="Image" src="https://github.com/user-attachments/assets/8ef81278-a3d4-41bf-8950-2ed6325a4791" />
same issue on 2.1.6 and windows 11 with bash.
+1
Thanks for the detailed write-up! This should be fixed in tomorrow's release
*today's releae, right @timgu0 ?
Just waiting to check if it fixed
+1
+1
<img width="163" height="310" alt="Image" src="https://github.com/user-attachments/assets/55761015-2d97-4d69-9d03-2b0a17a84cd4" />
Same
2.1.7 version has fixed this issue.
2.1.7 does not fix this on windows at least.
Please reopen the issue, it is not fixed
Still an issue on 2.1.7 on Windows, yeah
seems fixed on windows
not yet for now, but it may be just needs to be arrived to me
On windows 11 it doesnt generate tmp anymore for me.
still happening for me on windows 2.1.7
Checking again now. If it's still happening to you, do you mind sharing a screenshot of your claude with the version number and the path of these tmp files?
<img width="1443" height="533" alt="Image" src="https://github.com/user-attachments/assets/a0713f5f-d0ef-4481-a5ac-2e1aa027c8aa" /> yep this is still happening
Temporary Workaround: Automated Cleanup
I've created an automated solution that cleans up these temp files automatically: tmpclaude-auto-cleanup
Quick Setup (30 seconds)
Just copy the prompt from
CLAUDE_PROMPT.mdand paste it into Claude Code. Claude will configure everything automatically.What It Does
git statusManual Setup
Full instructions and manual configuration files available in the repo.
---
Note: This is a workaround until the core issue is fixed in Claude Code CLI. The real bug still needs to be addressed by Anthropic.
This is the reason people love opencode than claude code and yet they force claude code
Yes, the problem still persists. I have just cleaned up dozens of CWD files.
os: win10
2.1.7 still happening here
win64 (windows 11)
Dev Drive (not wsl)
The best cleanup is never creating the files in the first place.
Seems that an environment variable is the fix. CLAUDE_CODE_TMPDIR
https://github.com/anthropics/claude-code/issues/18008#issuecomment-3751048803
And yet new issues suggesting that are being closed as duplicates of this one. #31024
Great analysis. Until the upstream fix lands, here's a simple cleanup approach:
Option 1: Stop hook (cleans up when Claude Code exits)
Option 2: Cron (catches sessions that crash without triggering Stop)
The Stop hook handles normal exits; the cron catches edge cases. Both are safe — these files are only read once immediately after each Bash call, so deleting files older than 60 minutes has zero risk of interfering with active sessions.
A Stop hook can clean these up at session end:
Add to
~/.claude/settings.json:Only deletes files older than 60 minutes so it won't affect active sessions. Or install it with:
You can clean these up automatically with a Stop hook that runs when each session ends:
This runs automatically when Claude Code stops (session end, Ctrl+C,
/exit). The-mmin +5avoids deleting files from a concurrent session.One-liner to clear the backlog:
If you want belt-and-suspenders, add a cron job alongside the hook:
The Stop hook handles normal session ends; cron catches crashed sessions where Stop doesn't fire.
/tmp/gh-comment-8856.md