When "/tmp" and $TMPDIR directory are cleaned up in macOS?

When "/tmp" and $TMPDIR directory are cleaned up in macOS?

I know about removing files and directories in /tmp directory in boot time. Is it right?

In addition, are there other times in trying to clean up the directory?

Answered by DTS Engineer in 207917022

Thanks for the explanation.

The temporary directory cleanup is actually run by infrastructure inherited from BSD. You can read about it in the

periodic.conf
man page and by reading
/etc/periodic/daily/110.clean-tmps
and
/etc/defaults/periodic.conf
. It looks like
daily_clean_tmps_days
defaults to 3, meaning that a file gets deleted if it hasn’t been accessed in three days.

Keep in mind that the specific details aren’t considered API, and can vary from release to release and platform to platform. For example, iOS has a very different system for handling temporary files.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Why do you care? Specifically, are you asking from an API perspective (as a developer, when is it safe to leave files in the temporary directory?) or from a user perspective (when will the system recover that disk space?).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

My app creates and check some files in "/tmp" or $TMPDIR.


It's because files tried to be saved in user selective path are stored temporary and because some files to be used flags whether my app logs its status or not. To protect human error, for example, our supporter team's forgetting removing a log flag file, there files stores in /tmp or $TMPDIR


So, for app's operations and maintenace, I wanna know the time.


If only every boot time, I'm OK. But not, there are somethings to be considered such as safer code and explaining these things to techincal support team and testers who want to log my app status long time.

Accepted Answer

Thanks for the explanation.

The temporary directory cleanup is actually run by infrastructure inherited from BSD. You can read about it in the

periodic.conf
man page and by reading
/etc/periodic/daily/110.clean-tmps
and
/etc/defaults/periodic.conf
. It looks like
daily_clean_tmps_days
defaults to 3, meaning that a file gets deleted if it hasn’t been accessed in three days.

Keep in mind that the specific details aren’t considered API, and can vary from release to release and platform to platform. For example, iOS has a very different system for handling temporary files.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you!!

they get cleaned along with a lot of other crap every time i run my bash script, sudo **** garbage


1 cleans out the internet cache,

2 rebuild the mail database, delete downloaded just

3 nukes every .DS_Store on any drive

4. nukes every .Trash on every drive


you should just download Onyx, Maintenance or Deeper

@eskimo It doesn't look to me like $TMPDIR is referenced in periodic.conf by default; only /tmp is.

You would have to override daily_clean_tmps_dirs to include it.

i finally found the actual answer to the TMPDIR content deletion behavior after observing my un-accessed 3-day old files in TMPDIR getting deleted at 3:35am. it's using a different mechanism from the /tmp/ directory. and if you don't want your per-user files to get deleted and still want it in /var/folders/ (and don't want to use the home directory ~/Library/Caches/), you might want to consider using the "cache" (DARWIN_USER_CACHE_DIR) directory instead.

"3:35am" and "$TMPDIR" were mentioned in the page below.

http://www.mobileread.mobi/forums/showthread.php?t=273999

"on Mac OS X, there is a dirhelper daemon that looks for files not accessed within 3 days inside the official temp directory and will delete them even if the app using them is still running."

the dirhelper daemon runs at 3:35am and deletes files that didn't not get accessed for 3 days.

/System/Library/LaunchDaemons/com.apple.bsd.dirhelper.plist

% getconf DARWIN_USER_TEMP_DIR 
/var/folders/xx/xxxx_xxxx/T/

% getconf DARWIN_USER_CACHE_DIR     
/var/folders/xx/xxxx_xxxx/C/

% man confstr
 ...
 _CS_DARWIN_USER_TEMP_DIR
         Provides the path to a user's temporary items directory. The directory will be created it if does
         not already exist. This directory is created with access permissions of 0700 and restricted by the
         umask(2) of the calling process and is a good location for temporary files.

         By default, files in this location may be cleaned (removed) by the system if they are not accessed
         in 3 days.

 _CS_DARWIN_USER_CACHE_DIR
         Provides the path to the user's cache directory. The directory will be created if it does not
         already exist. This directory is created with access permissions of 0700 and restricted by the
         umask(2) of the calling process and is a good location for user cache data as it will not be
         automatically cleaned by the system.

         Files in this location will be removed during safe boot.
 ...

% cat confstr.c 
// cc confstr.c -o confstr -mmacosx-version-min=10.11 -arch x86_64 -arch arm64

#include <unistd.h>
#include <stdio.h>
#include <sys/syslimits.h>

int main() {
    char buf[PATH_MAX];
    size_t len = PATH_MAX;
    size_t s = confstr(_CS_DARWIN_USER_TEMP_DIR, buf, len);
    printf("_CS_DARWIN_USER_TEMP_DIR(len:%d): %s\n", (int)s, buf);
    s = confstr(_CS_DARWIN_USER_CACHE_DIR, buf, len);
    printf("_CS_DARWIN_USER_CACHE_DIR(len:%d): %s\n", (int)s, buf);
    return 0;
}

% ./confstr
_CS_DARWIN_USER_TEMP_DIR(len:50): /var/folders/xx/xxxx_xxxx/T/
_CS_DARWIN_USER_CACHE_DIR(len:50): /var/folders/xx/xxxx_xxxx/C/
When "/tmp" and $TMPDIR directory are cleaned up in macOS?
 
 
Q