142

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
  • 1,681

1 Answers1

172

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

Long answer:

This was changed significantly in MacOS Sonoma 14

Prior to Sonoma, it works like this:

  1. launchd sets up /usr/sbin/periodic daily to run every 86400 seconds (1 day) (definition in /System/Library/LaunchDaemons/com.apple.periodic-daily.plist). Older versions of Mac OS scheduled it to run at 3:15 am each day.
  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:".

In MacOS Sonoma, this was migrated to be controlled more directly with launchd

  1. There is a new launch daemon configured specifically for cleaning /tmp in /System/Library/LaunchDaemons/com.apple.tmp_cleaner.plist
  2. Using StartCalendarInterval, it runs /usr/libexec/tmp_cleaner daily at hour 0 (i.e. midnight)
  3. tmp_cleaner is a shell script that is very similar to 110.clean-tmps used pre-Sonoma but it does not provide any easy way to override the 3 day setting. You'd have to manually edit this file (and do it again on OS update0
Doug Harris
  • 27,811
  • 13
    Note in 10.7 periodic.conf has moved to /etc/defaults . – Dan Nov 04 '11 at 00:47
  • 2
    Does the same apply to $TMPDIR? – ccnokes Jul 28 '16 at 21:38
  • 1
    No, I don't believe that it does. There's no reference to $TMPDIR in any of the config files mentioned. – Doug Harris Jul 29 '16 at 17:12
  • 1
    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 Aug 21 '17 at 22:50
  • 6
    n.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 Feb 16 '18 at 14:59
  • 6
    Using OSX 10.14.5, can confirm that /tmp seems to be wiped out on reboot. – karlos Jul 23 '19 at 21:53
  • 2
    Instructions appear to be current as of Aug-2019 / MacOS Mojave 10.14.6. – Daniel Wille Aug 05 '19 at 14:45
  • 2
    I dug into find more details on when this is run (I'm currently on 10.14.6 but the latest version of MacOS is 10.15.6). As I noted on Feb 16, 2018, periodic daily is no longer scheduled to run at 3:15 a.m. It's now defined to run at an interval of 86400 seconds (1 day). I've analyzed the data in /var/log/daily.out and see that it has run at any hour of the day. Since the beginning of 2020, the average time between runs has been ~26.5 hours, minimum 6 hrs and max at 10+ days (when I've been on vacation). – Doug Harris Sep 22 '20 at 14:59
  • 1
    Good as of August-2021 / macOS Big Sur – cswilby Aug 17 '21 at 00:50
  • I'm assuming that if you have a long-running program that actively has a file open, then that file won't get deleted by the periodic sweep? PS from a single test, /tmp is deleted on boot on macOS 12.4 Monterey – Nick Crews Jul 16 '22 at 19:50
  • I’d guess that would update the access time, so the process that cleans out /tmp would not delete it. – Doug Harris Jul 17 '22 at 01:39
  • Still valid for macOS Monterey 12.4 – Doug Harris Jul 21 '22 at 18:36
  • Updated the answer with significant changes in MacOS Sonoma 14 – Doug Harris Jan 08 '24 at 16:08