How to delete entire TemporaryCachePath?


My application need to download lots of temporary file to a folder.

I put it on “Application.temporaryCachePath”.

and I want to delete it before Application.Quit

I’ve try:


new DirectoryInfo(Application.temporaryCachePath).Delete();

FileInfo[] files = new DirectoryInfo(Application.temporaryCachePath).GetFiles();
foreach(FileInfo file in files) {

but all of them don’t work…

is there anyway to delete temporary files?

This is an old question, but I was looking for the answer myself, so here’s my two cents.

You’re trying to delete the temporary cache directory, but you can’t because this directory is read-only. You need to only delete the files inside this directory. This answer is a slightly modified version from this stack overflow answer: c# - How to delete all files and folders in a directory? - Stack Overflow

public static void DeleteCache()
    string path = Application.temporaryCachePath;

    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);

    foreach (System.IO.FileInfo file in di.GetFiles())

    foreach (System.IO.DirectoryInfo dir in di.GetDirectories())