[BUG] Claude Code's Write tool stores files in ~/.claude/sessions/ which are then silently deleted by Claude Code's own cleanup

Status Fixed / completed
Reported on v2.1.76
Maintainer reply None cached
Activity 5 comments · opened Mar 15, 2026 · closed Mar 15, 2026

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:

  1. 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.
  1. cleanupPeriodDays deletes 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 was cleanupPeriodDays: 7, which compounded the default behavior.

What Should Happen

  1. The Write tool should warn or refuse when writing to internally managed directories (~/.claude/sessions/, ~/.claude/cache/, ~/.claude/file-history/).
  2. The documentation should explicitly list which ~/.claude/ subdirectories are safe for user files and which are internally managed.
  3. cleanupPeriodDays should 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

  1. Have Claude Code create a skill that writes .md files to ~/.claude/sessions/
  2. Use the skill multiple times over months
  3. Observe that files disappear silently over months without any warning or notification
  4. 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.

View original on GitHub ↗

5 Comments

Aconradty · 7 months ago

Additional detail: Immediate deletion, not just cleanupPeriodDays

The cleanupPeriodDays: 7 setting 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:

  • File written via Write tool → "File created successfully"
  • Verified via ls immediately after → file exists
  • Checked again minutes later → file gone
  • No unlink syscalls observed in sudo fs_usage -w -f filesys | grep sessions
  • Canary files created via echo > file from bash survived indefinitely in the same directory
  • Multiple Claude Code instances were running concurrently at the time (CLI terminal sessions + Cursor extension with Claude Code plugin)

This suggests there are two separate deletion mechanisms:

  1. cleanupPeriodDays — deletes old JSONL transcripts on startup (documented)
  2. Something else — deletes non-lock-file content from ~/.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 cleanupPeriodDays to matter.

Environment during immediate deletion:

  • 3 concurrent Claude Code CLI processes (PIDs 75571, 64752, 67620)
  • Cursor with anthropic.claude-code-2.0.20-darwin-arm64 extension active
  • macOS Darwin 25.3.0
github-actions[bot] · 7 months ago

Found 1 possible duplicate issue:

  1. https://github.com/anthropics/claude-code/issues/34210

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

yurukusa · 7 months ago

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):

  1. ~/.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.
  1. Any directory outside ~/.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 cache

Prevention via CLAUDE.md:

You can add a rule to your CLAUDE.md to prevent Claude from choosing managed paths:

## Storage Rules
- `~/.claude/sessions/`, `~/.claude/debug/`, `~/.claude/file-history/` are NOT permanent storage
- For persistent data, use `~/.claude/projects/*/memory/` or directories outside `~/.claude/`

This won't recover lost files, but it prevents recurrence. Claude Code reads CLAUDE.md at 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.

Aconradty · 7 months ago

Closing as duplicate of #34210, which has identified the exact root cause.

The concurrentSessionCleanup() function iterates all files in ~/.claude/sessions/ and runs parseInt(filename.replace(/\.json$/, ""), 10) on each. For date-prefixed files like 2026-03-15-soul-md.md, parseInt returns 2026. 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, while INDEX.md and non-digit-prefixed files survived.

One missing line: if (!/^\d+\.json$/.test(file)) continue;

github-actions[bot] · 6 months ago

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.