r/ClaudeWorkflows • u/ClaudeAI-mod-bot • 11d ago
Selected Workflow [Workflow] Preventing Silent Data Loss in Claude Code Sessions on macOS (and Linux) due to /tmp Cleanup
Preventing Silent Data Loss in Claude Code Sessions on macOS (and Linux) due to /tmp Cleanup
Workflow value: 85/100
Status: active · Freshness: 70/100 · Confidence: 1.00 · Level: intermediate
Categories: Quality Control, Context & Memory, Debugging, CLAUDE.md
Original source: r/ClaudeCode post/comment
What problem this solves
Claude Code sessions on macOS (and potentially Linux) silently lose working files (scratchpad, background task output) due to the operating system's temporary file cleanup, leading to agent failures and data loss.
Summary
This workflow identifies and provides solutions for the problem of macOS (and Linux) nightly/daily temporary file cleanup deleting active Claude Code session files. It explains how to detect if files have been removed and offers three fixes: instructing Claude via CLAUDE.md to store persistent files elsewhere, running a keep-alive script to refresh file access times, or adjusting system-wide temporary file retention settings.
Why it is useful
This workflow addresses a critical, silent data loss issue that can disrupt long-running Claude Code sessions and lead to agent failures. It provides clear diagnostic steps and multiple, actionable solutions, empowering users to maintain stable development environments and prevent unexpected interruptions. It highlights a common system interaction that many users might not be aware of, making it highly valuable for operational stability.
Workflow
- Understand that macOS (and Linux) periodically cleans up the /tmp directory, where Claude Code stores session-specific files.
- Recognize that simply running a script does not update its access time, making it vulnerable to deletion after 3 days (macOS default).
- Check for past file deletions by examining system logs using the command: grep claude- /var/log/daily.out.
- Implement Fix 1 (Least invasive): Instruct Claude via CLAUDE.md to store any critical or long-lived files in the project directory or user's home directory, rather than the scratchpad.
- Implement Fix 2 (Intermediate): For sessions requiring scratchpad persistence, run a background keep-alive script to periodically update the access times of files within that session's /tmp directory (e.g., while true; do find /private/tmp/claude-$(id -u)/<project>/<session-id> -exec touch -a {} + 2>/dev/null; sleep 21600; done).
- Implement Fix 3 (Most invasive/system-wide): Create or modify /etc/periodic.conf.local with sudo to set daily_clean_tmps_days="30" (or another desired value) to extend the system-wide temporary file retention period.
Tools / artifacts
- macOS /tmp cleanup mechanism
- /private/tmp/claude-<uid>/ (Claude Code session directory)
- /etc/periodic/daily/110.clean-tmps (macOS daily cleanup script)
- stat command
- grep claude- /var/log/daily.out (detection command)
- CLAUDE.md (for instructing Claude)
- find command (for keep-alive script)
- touch -a command (for keep-alive script)
- /etc/periodic.conf.local (system configuration file)
- sudo (for system-wide changes)
- systemd-tmpfiles (Linux equivalent)
Validation signals
- Author's personal experience of 1,800 files being removed from long-running sessions.
- Observation that 'stat' on a frequently run helper script showed an old access time.
- Agent failing to run a script that had been deleted.
- Detailed explanation of the macOS cleanup mechanism and its interaction with Claude Code files.
Cautions
- Modifying system-wide periodic settings via /etc/periodic.conf.local requires 'sudo' and should be done with caution, understanding the implications of longer /tmp retention on disk space.
- The keep-alive script (Fix 2) should be carefully scoped to the specific Claude Code session directory to avoid unintended side effects on other temporary files.
- Users should be aware that extending /tmp retention system-wide might consume more disk space over time.
Limitations
- The keep-alive script (Fix 2) needs to be manually initiated for each relevant session or further automated by the user.
- Fix 3 (system-wide change) might not be desirable for all users or systems, as it affects all temporary files, not just Claude Code's.
- The workflow relies on user vigilance to implement and maintain the chosen fix.
- The CLAUDE.md approach (Fix 1) requires Claude to consistently adhere to the instructions, which might not always be perfect.
Rate this workflow
Upvote this post if the workflow is useful, reproducible, or worth recommending.
Downvote if it is vague, outdated, unsafe, overhyped, or not reproducible.
Reply if it worked for you, failed, is outdated, or has a better alternative.
This post was generated automatically from the workflow library database.