When are per-user temp files in /var/folders/ removed?

Solution 1:

It depends on the type of files created:

For files that the application has created as temporary files, they're up for removal when they have not been accessed for 3 days.

For files that the application has created as cache files, they're not normally automatically removed. Instead they can be removed when performing a safe boot (hold down the shift key while booting your Mac).

And no, as long as the application keeps the create file open (i.e. it has a reference to the file descriptor), it should never be the case that the file is removed from the file system before the application quits.

Note that application can and do work with files that are not visible for a regular user. I.e. if your application opens a file and keeps it open, the user can delete the file and it will disappear from directory listings - however the file is still there and the contents can be accessed by the application without problems. When the application closes the file (or the application is closed) - the file is permanently deleted from the file system.