In macOS, how often is /tmp deleted?

97

29

How often is /tmp deleted? I thought it was cleaned out boot time and every three days if you don't reboot, but I can't find any authoritative source for this.

I'm running the latest 10.6. (I suppose I would be interested in changes in this behavior in previous OS versions.)

zekel

Posted 2010-09-10T15:31:27.103

Reputation: 1 171

2How about the per-user temporary folders in /var/folders/ ? – adib – 2018-09-10T23:12:52.503

Answers

124

Short answer: by default, files that aren't accessed in three days are deleted from /tmp

Long answer:

  1. launchd sets up /usr/sbin/periodic daily to run every day at 3:15 am (definition in /System/Library/LaunchDaemons/com.apple.periodic-daily.plist).
  2. /usr/sbin/periodic daily runs the scripts in /etc/periodic/daily, including 110.clean-tmps.
  3. 110.clean-tmps uses find to delete files not accessed (using the -atime and -mtime parameters to find) in $daily_clean_tmps_days days which aren't listed in $daily_clean_tmps_ignore.
  4. Those two variables (and other related variables) are defined in /etc/periodic.conf. (Since OS 10.7 there is a file /etc/defaults/periodic.conf which defines the defaults, but for overwriting these defaults you should still create /etc/periodic.conf – see the periodic.conf(5) manual page.) If you haven't modified this file, $daily_clean_tmps_days is set to 3.

periodic daily logs its output to /var/log/daily.out. Files deleted from /tmp are logged under the heading "Removing old temporary files:".

Doug Harris

Posted 2010-09-10T15:31:27.103

Reputation: 23 578

1Does the same apply to $TMPDIR? – ccnokes – 2016-07-28T21:38:13.353

1No, I don't believe that it does. There's no reference to $TMPDIR in any of the config files mentioned. – Doug Harris – 2016-07-29T17:12:36.053

I have some indications that files are also cleared from /tmp on restart of the machine (I had a few gig of Blender renderings in /tmp that went poof and I think that they left this world when I restarted the machine) – Reb.Cabin – 2017-08-21T22:50:57.593

3n.b. This answer is 7.5 years old at this point, macOS is now at 10.13 and I'm using 10.11. While the short answer is still correct (3 days), some of the details have definitely changed. For example, in 10.11, I don't see the line that schedules this for 3:15 a.m. in /System/Library/LaunchDaemons/com.apple.periodic-daily.plist.

I've not had the time to learn more about the boot process to determine how /tmp is cleared at boot time (per the comment by @Reb.Cabin above) – Doug Harris – 2018-02-16T14:59:15.220

4Using OSX 10.14.5, can confirm that /tmp seems to be wiped out on reboot. – karlos – 2019-07-23T21:53:29.910

1Instructions appear to be current as of Aug-2019 / MacOS Mojave 10.14.6. – Daniel Wille – 2019-08-05T14:45:45.100

11Note in 10.7 periodic.conf has moved to /etc/defaults . – Dan – 2011-11-04T00:47:32.477