Are files in the temporary folder automatically deleted? Are files in the temporary folder automatically deleted? windows windows

Are files in the temporary folder automatically deleted?


FileOptions.DeleteOnClose will cause the file to be deleted automatically when closed. This also works if the program is terminated by an exception.

For example, as mentioned in this answer:

using (FileStream fs = new FileStream(Path.GetTempPath() + "foo.bar",       FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None,       4096, FileOptions.RandomAccess | FileOptions.DeleteOnClose)){    // temp file exists}// temp file is gone


No, you will need to manually delete the file. Path.GetTempPath() just gives you the folder path to the temp folder.


Starting with Windows 10, the answer is posibly yes - depending on the machine configuration and the amount of free space on the drive hosting the TEMP folder.

Specifically, Storage Sense can arbitrarily delete files from the TEMP folder (I found that out the hard way) if enabled by the user. And from what I can tell, it will self-enable on low disk space.