Depends on operating system.
How and when these files are removed? Is there any configuration on the operating system regarding this?
On Windows do not have anything automatic, you should remove manually. Cleaning what is temporary should be enforcement responsibility. Putting something in there just means it can disappear at any time without application control. Obviously if the file is in use no one can remove it.
On Linux it depends on where you place it and the distribution. On /tmp
, although it is recommended to delete when there is boot, nothing guarantees that it is done in its distribution. No var/tmp
stay as long as necessary until some intervention of the application or the "owner" of the machine, like Windows.
If you don’t add a specific application to take care of it and your application doesn’t take care of it, in general it will fill.
This is just a convention not to leave temporary files scattered everywhere and you know that erasing there gets rid of them. Too bad many programmers ignore this recommendation and spread files everywhere.
Does it have anything to do with system reboot? If so, what about servers, which are usually rarely rebooted?
It’s on Linux, but anyway it’s your problem to fix it, so it doesn’t matter if it’s a server.
Is there any way to flag on the operating system (at least on Windows and Linux) that other folders will also be considered temporary?
As far as I know there is no standard, but in Linux it can always be moved if the person wants a lot and is empowered. And it doesn’t even matter if something like this exists. Windows allows you to change the location of the folder.
How to use this folder in my application?
This was the most important question not asked in a programming Q&A.
On Linux it is safe to always use /tmp
and /var/tmp
to put your temporary files.
In Windows the correct is to see where the folder is already that it can be configured by the user (there can only be one), through API provided in the language you are using or by the environment variable TEMP
.
If you do not do so and feel you should put them elsewhere at least ensure that they are cleaned in any circumstances, even when the application breaks.