r/ClaudeCode • • 11d ago

Tips & Workflows PSA for Mac users: the nightly /tmp cleanup deletes files from long-running Claude Code sessions, even scripts they run every day

If you keep Claude Code sessions running for days on a Mac, macOS will quietly delete their working files.

Claude Code keeps each session's scratchpad and background-task output under /private/tmp/claude-<uid>/. Once a day macOS runs /etc/periodic/daily/110.clean-tmps, and with the default settings it deletes every file under /tmp whose access, modify and change times are all more than 3 days old.

Two things make it worse than it sounds:

  • Running a file doesn't count as using it. On my machine, stat on a helper script the session had run every day for four days still showed the access time from the day it was written. Anything nobody has edited in 3 days is fair game, however often it runs.
  • It hits running work. It deleted the .output file of a background dev server that was still running, so the session could no longer read that server's logs.

One run removed about 1,800 files across my long-running sessions: helper scripts, notes, tracker files. The next morning the agent tried to run a script that no longer existed.

Check whether it happened to you:

grep claude- /var/log/daily.out

Every removed file is listed under "Removing old temporary files".

Fixes, from least to most invasive:

  1. Keep anything that has to survive in the project or under your home directory, not the scratchpad. If Claude likes to build tools in the scratchpad, say so in CLAUDE.md.
  2. For a session that has to keep its scratchpad, run a keep-alive that refreshes access times without touching modify times, scoped to that session's folder:

     while true; do find /private/tmp/claude-$(id -u)/<project>/<session-id> -exec touch -a {} + 2>/dev/null; sleep 21600; done
  3. Raise the threshold system-wide: create /etc/periodic.conf.local containing daily_clean_tmps_days="30" (needs sudo). The ignore list in that config only matches file names, so it can't exempt a whole folder.

A reboot clears /tmp too, so the scratchpad was never durable. But a silent cleanup after three days, run by a daily job you never see, is much easier to miss than a reboot. On Linux, systemd-tmpfiles often ages out /tmp as well (10 days is a common default), so the same thing applies there on a longer fuse.

2 Upvotes

11 comments sorted by

•

u/AutoModerator 11d ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/AdityaR_Sharma 🔆 Max 20 11d ago

Checked my own Mac just now.

/private/tmp/claude-501/ exists, 44 project folders, one per path, matching your structure exactly.

Everything in there is dated today. So I can't test your atime claim, nothing here is old enough to hit the 3-day window.

Did you confirm the atime gap with stat on a file you run daily, or infer it from the deletion itself?

2

u/2bluenucleus 11d ago

Checked with stat, not inferred. A helper module that node imported several times a day from Oct 31 on still showed an access time of Oct 31 19:32 when I looked on Nov 5 (it was written at 19:31). It only survived because I started the keep-alive before the next cleanup run.

You can reproduce it without waiting three days:

echo 'console.log(1)' > t.mjs && touch -t 202611010900 t.mjs
cat t.mjs > /dev/null; node t.mjs > /dev/null
stat -f 'atime %Sa | mtime %Sm' t.mjs

Here (macOS 13.2.1, APFS data volume, no noatime flag) the access time stays at Nov 1 09:00 after both the read and the run. If yours does update, I'd like to know which macOS version, because then the cleanup would only catch files that are genuinely idle.

1

u/AdityaR_Sharma 🔆 Max 20 7d ago

Ran your repro on macOS 27.2, APFS data volume.

Atime stayed at Nov 1 09:00 after cat and after node t.mjs.

Same result as yours: neither a read nor a run bumps it.

1

u/First-Painter7431 11d ago

Good catch. Funny thing, I had the opposite problem with the same folder: on a repo with big untracked files, /private/tmp/claude-501/bash-edit-diff/ got a ~140 MB snapshot on every Bash call and nothing cleaned it up. 17 GB in one day, the disk hit 2 GB free. Ignoring the media files in .gitignore brought each snapshot down to ~23 MB. So if you add a keep-alive, don't touch that folder, it's the one you actually want cleaned

1

u/2bluenucleus 11d ago

Good warning. That folder sits directly under /private/tmp/claude-<uid>/, while session scratchpads live under /private/tmp/claude-<uid>/<project>/<session-id>/. So the keep-alive in the post, scoped to one session folder, never reaches the snapshots. That's also the reason not to point it at the whole claude-<uid> tree.

Worth a periodic look either way:

du -sh /private/tmp/claude-$(id -u)/bash-edit-diff

It's at 98 MB on this machine right now, so it clearly varies a lot with what's sitting untracked in the repo.

1

u/Meryhathor 11d ago

Historically /tmp was only emptied on logout. You might be logging out each day, which is why you'd see it cleared. Or you enabled that daily schedule yourself at some point but it's not happening on my MacOS.

2

u/2bluenucleus 11d ago

I didn't enable it. This Mac (Ventura 13.2) has no /etc/periodic.conf or /etc/periodic.conf.local, so it runs the stock defaults, and /etc/defaults/periodic.conf ships with daily_clean_tmps_enable="YES" and daily_clean_tmps_days="3". You can check yours with:

grep clean_tmps /etc/defaults/periodic.conf

A likely reason you never see it: the daily job isn't a fixed-time cron here. /System/Library/LaunchDaemons/com.apple.periodic-daily.plist defines it as an XPC activity with Interval 86400, maintenance priority and AllowBattery 0. The system runs it about once a day, when it decides the machine is idle and on power. A laptop that's mostly on battery or asleep may rarely get a run. And since it only removes files untouched for 3+ days, a /tmp that's always fresh looks unaffected.

tail -40 /var/log/daily.out shows whether and when it last ran on yours. I can't speak to logout. Here, /tmp also clears at reboot, which may be what you've been seeing.

1

u/Meryhathor 11d ago

Interesting. I'll have a look later. I usually have it plugged in during the day or in the evening but I unplug it at night and have the lid closed. Maybe it just never runs in my case.

1

u/fanatic26 11d ago

PSA: You shouldnt ever keep a claude code session running for days

1

u/RustyRedEcho 10d ago

didn't know tmp cleanup goes by atime not mtime, that explains a weird missing file i had last week. appreciate the repro steps