[BUG] Claude Code's Write tool stores files in ~/.claude/sessions/ which are then silently deleted by Claude Code's own cleanup
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
Claude Code's own Write tool happily writes .md files to ~/.claude/sessions/. There is no warning, no restriction, and no documentation that this directory is internally managed. Claude Code then silently deletes those files through its cleanup process.
In my case, a custom /sessions skill (created by Claude Code itself) stored session log markdown files in ~/.claude/sessions/ for months. Over ~196 session logs were written there between October 2025 and March 2026 — post-session reflections with quality ratings, root cause analyses, key decisions, and lessons learned. All of them were silently deleted. Only 17 were recoverable from JSONL transcript history. The rest are permanently lost.
The irony: Claude Code chose ~/.claude/sessions/ as the storage path when building the skill. It was never flagged as an internal directory. No warning was shown during any of the ~196 Write operations.
Root Cause
Two interacting issues:
- No guardrails on Write tool paths. The Write tool allows writing to any path under
~/.claude/without warning that some subdirectories are internally managed and subject to cleanup.
cleanupPeriodDaysdeletes more than documented. The documentation says it affects "sessions inactive for longer than this period." In practice, files placed in~/.claude/sessions/by the Write tool are also affected. My setting wascleanupPeriodDays: 7, which compounded the default behavior.
What Should Happen
- The Write tool should warn or refuse when writing to internally managed directories (
~/.claude/sessions/,~/.claude/cache/,~/.claude/file-history/). - The documentation should explicitly list which
~/.claude/subdirectories are safe for user files and which are internally managed. cleanupPeriodDaysshould not delete files it didn't create. User-placed files in managed directories should be preserved, or at minimum the directory's managed status should be documented.
Related Issues
- #22547 — [BUG] data loss through bad default settings (high-priority)
- #15935 — [Bug] Project files deleted despite cleanupPeriodDays set to 1825
- #16970 — [BUG] claude is losing chat history
Steps to Reproduce
- Have Claude Code create a skill that writes
.mdfiles to~/.claude/sessions/ - Use the skill multiple times over months
- Observe that files disappear silently over months without any warning or notification
- No warning is shown at any point
Error Messages/Logs
No errors. Files are silently deleted. The Write tool reports "File created successfully"
every time. The cleanup produces no user-visible output.
Claude Model
Opus (claude-opus-4-6)
Is this a regression?
No, this never worked
Last Working Version
_No response_
Claude Code Version
2.1.76
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Terminal.app (macOS)
Additional Information
Impact: ~179 session log files permanently destroyed over ~5 months (October 2025 – March 2026). Months of curated reflections, failure analyses, and decision records. 17 partially recovered from JSONL history. The remaining ~162 are unrecoverable — the JSONL transcripts containing the Write tool calls were also deleted by cleanupPeriodDays.
Workaround: Store user files in ~/.claude/session-logs/ or any other directory not managed by Claude Code internally.
5 Comments
Additional detail: Immediate deletion, not just cleanupPeriodDays
The
cleanupPeriodDays: 7setting explains why older files disappeared. But during this session, files written via the Write tool to~/.claude/sessions/were deleted within minutes — not after 7 days.Timeline:
lsimmediately after → file existsunlinksyscalls observed insudo fs_usage -w -f filesys | grep sessionsecho > filefrom bash survived indefinitely in the same directoryThis suggests there are two separate deletion mechanisms:
cleanupPeriodDays— deletes old JSONL transcripts on startup (documented)~/.claude/sessions/during active use (undocumented, not caught by fs_usage, possibly triggered by concurrent instance lifecycle)The second mechanism is the more dangerous one because it defeats any retention setting. Files don't survive long enough for
cleanupPeriodDaysto matter.Environment during immediate deletion:
anthropic.claude-code-2.0.20-darwin-arm64extension activeFound 1 possible duplicate issue:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
This is a real and painful failure mode. I've seen the same pattern and want to share what works as a safe alternative.
Safe persistent storage paths (verified stable across sessions):
~/.claude/projects/<project-key>/memory/— This is the intended location for persistent user data. Files here survive cleanup, compaction, and session rotation. Claude Code's own auto-memory system uses this path.~/.claude/entirely — e.g.,~/ops/,~/proof-log/,~/.config/your-tool/. If your skill writes reflections or logs, point it here instead.Directories to avoid:
~/.claude/sessions/— internally managed, subject to cleanup~/.claude/debug/— transient debug data~/.claude/file-history/— internal file snapshots~/.claude/cache/— ephemeral cachePrevention via CLAUDE.md:
You can add a rule to your
CLAUDE.mdto prevent Claude from choosing managed paths:This won't recover lost files, but it prevents recurrence. Claude Code reads
CLAUDE.mdat session start and respects these rules consistently.For the core issue: Agree that the Write tool should at minimum warn when targeting internally managed directories. A simple path check against a deny-list (
sessions/,cache/,debug/,file-history/) would catch this class of silent data loss.Closing as duplicate of #34210, which has identified the exact root cause.
The
concurrentSessionCleanup()function iterates all files in~/.claude/sessions/and runsparseInt(filename.replace(/\.json$/, ""), 10)on each. For date-prefixed files like2026-03-15-soul-md.md,parseIntreturns2026. Since PID 2026 isn't running, the file is deleted as a "stale PID lock."This is why all ~179 of our session logs (all named
YYYY-MM-DD-topic.md) were silently destroyed over 5 months, whileINDEX.mdand non-digit-prefixed files survived.One missing line:
if (!/^\d+\.json$/.test(file)) continue;This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.