I know that system /tmp and $TMPDIR are cleaned periodically and on reboot, but what about /tmp directory inside app containers?
Because it looks like on macOS Sonoma it is not cleaned automatically and I was wondering if it is by design? And what should I do about it? Should I delete these files manually for existing users or is it possible to somehow nudge macOS into doing it?
It looks like macOS Sonoma doesn't delete /tmp files in containers periodically or on reboot, like it does for system /tmp or $TMPDIR.
Is it a bug or is it in on purpose?
Both and neither. It's not a "bug", in the sense that nothing has actually "failed" that's causing this behavior. However, it's not "on purpose", in that you also shouldn't assume that the system will never delete your container's tmp.
The issue here is basically just about implementation complexity. Deleting the system wide "/tmp" is fairly simple to implement- the directly is globally visible so the system always knows where it is and can delete it at will. You do need to be careful about disrupting other process/apps, but it's basically a straightforward process.
Deleting <container>/tmp is far more complicated. Home directories do not exist in specific locations- /Users/ is the default, but the system will let you put them "anywhere" you want. When/if they're accessible to the larger system is also not well defined (network home directories, file protection, external volumes, etc...). The number of directories can also be very large, making the data management process more complicated.
You can bypass most of these issues by tying the clean out process to user login (so the deletion only occurs while the user is logged in), but that means it's hard to avoid disrupting the user. I don't recall exactly what changes occurred on what system version, but I believe Ventura does delete "<container>/tmp" and that it's likely Sonoma did not.
That leads to your question here:
Our app didn't delete its temp files and some users have quite a few of them. We will make the fix so that we clear after ourselves in the future but should we delete temp files manually on start for existing users?
My recommendation has always been that apps should delete Cache/tmp data as soon as they "know" it will not longer be useful. Your app is the only component that can make that determination and there's no reason to waste resources on storage that doesn't provide any benefit. That can be a tricky determination to make for apps like web browser or content viewer (where, in theory, the cache could be useful "forever"), but it sounds like your usage is fairly simple. In that case, "at startup" is a reasonable option, particularly as a secondary mechanism to cleanup data left over from previous app runs where your app couldn't delete the data during the run itself.
One other macOS specific note is that apps need to be aware/proactive about this sort of self-cleanup/maintenance than they would be on iOS (or any of our other systems). On macOs, the system is far more configurable and apps have more power to control/disrupt the default system behavior. It's easy to say that a given system version "should" delete the data, but that doesn't matter very much is some other component or combination of other factors mean that it doesn't occur the way you might expect.
-Kevin Elliott
DTS Engineer, CoreOS/Hardware