do files in the temporary folders get automatically deleted?

We are thinking to make our web application hosted on a windows server 2003 machine create temporary files, and "leave" it there as such. We won't manually take any action on them. So will these files get automatically deleted? Or is there some sort of policy we have to configure - like these files should get deleted after 30 days or so...

Update:

A production server will hardly get shutdown. What to do then if the files have to be deleted somehow?


No, there's nothing turned on by default in Windows that will automatically delete temp files. Not on shutdown, not on reboot, not ever.

The Disk Cleanup tool has an option to do that when its run.

The proper way to handle this is for your web application to clean up after itself in some way. You might have it delete orphaned files when a user logs out, for example. Or a (small) random chance that a "cleanup sweep" will be made on every request. Or a scheduled task that must be installed and configured to do cleanup. Or a scheduled task to run Disk Cleanup.

Do not go about just "leaving stuff" in the temp folder in the hopes that something will come along and clean up your mess for you.

Also, when you do go about cleaning up your files, only touch the files that you've created and no others. Other applications may need those files. If tracking your own files is too much trouble, create a folder that you can safely empty at will.


As of Windows 10, the answer now seems to be "yes". Windows 10 includes a feature called Storage Sense which among other things may delete older files in the temporary folder.

Whether or not it will do this can be configured in settings. From experience with my customers it is definitely the case that this can be turned on without end users necessarily being aware of it - perhaps either as a system default or by admin configuration.

There are a number of different categories that Storage Sense may include in its cleanup procedures. Here is a screenshot of the Storage Sense settings:

enter image description here

The temporary files category seems to be the general use temp files on the computer, the relevant portion reads:

Temporary Files

Apps can store temporary information in specific folders. These can be cleaned up manually if the app does not do it automatically.

(This text appeared in Windows Settings at one time, though as of Nov 2021 it seems to be no longer is shown.)

I've seen this occur to files which were "old" - based on their modification date - but not ancient. I don't have a specific example at hand but the files were only a few months old in cases I had seen in the past.

This feature does see to affect files in %TEMP% which seems typically to be %APPDATA%\Local\Temp ie C:\users\username\AppData\Local\Temp. I'm not sure if it can affect other locations, but I wouldn't be surprised.

Ref.: https://techcommunity.microsoft.com/t5/storage-at-microsoft/windows-10-and-storage-sense/ba-p/428270


Because you are speaking of a server with high availability, a scheduled task is the best way to go. You could do something as simple as a vbscript or batch file that finds all files over a certain date and purges them. Adjust the frequency of the schedule based on how quickly the temp files accumulate in order to minimize the load on the server on any given run.

Thomas Maurer has a powershell script that allows a pretty high level of flexibility, such as specifying the age of the files to be removed, the folder they are to be removed from, etc.

UPDATE: Thomas Maurer's site isn't loading for me, so I ran a google search for "powershell delete files older than 30 days" and the results look promising.