[Feature Request] Dedicated ~/.claude/tmp/ directory with session-scoped temp file management

Resolved 💬 2 comments Opened Mar 25, 2026 by Jodre11 Closed Mar 25, 2026

Problem

When Claude Code needs to write temporary files during a session (tool output, diffs, commit message drafts, XML reports from external tools, etc.), it uses /tmp/, $TMPDIR, or /var/folders/ with no consistent naming convention. This causes practical problems:

  • Collisions between concurrent sessions — multiple Claude sessions writing generic filenames like inspectcode-output.xml to /tmp/ overwrite each other
  • No cleanup of model-directed temp files — files Claude chooses to create (e.g. tool output it needs to parse) are never cleaned up (#17609, #17664, #17673)
  • Permission friction — users must grant Bash/Write permissions for system temp directories, which are broad and grant access to files from other processes

Note: this proposal addresses model-directed temporary files — where the model (or a skill/command) chooses to write a temp file as part of a workflow. It does not address Claude Code runtime temp files managed by the harness itself (Bash tool capture files, task output streams, worktree placement), which have their own bugs requiring runtime fixes (#30583, #33837, #35121).

Proposed Solution

Introduce ~/.claude/tmp/ as the dedicated temp directory for model-directed temporary files, with session-scoped filenames.

Convention

  • Location: ~/.claude/tmp/
  • Naming: {session-id}-{purpose}.{ext} — e.g. a1b2c3-inspectcode-output.xml
  • Lifecycle: each session creates files with its own prefix and cleans them up on completion

Benefits

  • User-scoped — no system-wide permission issues, unlike /tmp/ on shared machines
  • Session-scoped naming prevents collisions — the session ID prefix makes concurrent access safe
  • Discoverable — developers can inspect ~/.claude/tmp/ to debug session issues
  • Scoped permissions — users can grant Claude read/write/delete access to ~/.claude/tmp/ without opening up /tmp/ or $TMPDIR broadly. This is a tighter, more auditable permission grant
  • Persistent by default — unlike /tmp/ which is cleared on reboot, files in ~/.claude/tmp/ survive restarts. This is useful for debugging (you can inspect what a crashed session left behind) but is also a risk — without active cleanup, files accumulate indefinitely

Trade-offs to consider

  • Persistence is a double-edged sword. /tmp/ being cleared on reboot provides free garbage collection. ~/.claude/tmp/ needs explicit cleanup — either per-session (on normal exit) or via a retention policy (for crashed sessions). Without this, leaked files grow unbounded on the user's home partition.
  • Home directory disk pressure. /tmp/ is often on a system-managed partition. ~/.claude/tmp/ lives on the user's home directory, which may be a smaller SSD. Large temp files (e.g. verbose XML reports) could contribute to disk pressure. A size limit or warning threshold would mitigate this.
  • Not all users version-control ~/.claude/. The gitignore for ~/.claude/tmp/ is only relevant if ~/.claude/ is a git repo (which is not the default). For most users, the directory simply needs to exist and be writable — no git interaction required.

Implementation Sketch

  1. Claude Code creates ~/.claude/tmp/ on startup if it doesn't exist
  2. Expose a CLAUDE_TMP environment variable (or similar) so skills, commands, and CLAUDE.md instructions can reference it portably
  3. Model-directed temp file operations use ~/.claude/tmp/{session-id}-*
  4. On normal session end, files matching the current session prefix are deleted
  5. A configurable retention policy (similar to cleanupPeriodDays for transcripts) handles orphaned files from crashed sessions
  6. Consider a cleanupTmpOnBoot option for users who want /tmp/-like auto-expiry behaviour

User-Side Workaround (Current)

This can be implemented today via CLAUDE.md instructions, PreToolUse hooks (to deny writes to /tmp/ and redirect), and permission rules — but it requires manual setup per user and relies on the model consistently following instructions. A built-in solution would be more reliable.

Related Issues (model-directed temp files)

  • #17609 — tmpclaude-*-cwd files not cleaned up (Windows)
  • #17664, #17673 — Excessive tmpclaude-*-cwd temp files in working directory
  • #17814 — Feature request: auto-cleanup tmpclaude-* files
  • #36165 — Permission denied on wrong temp folder

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗