I’m saving some files to Application.persistentDataPath and now I would like the ability to clear out everything I’ve saved there at once. What’s a good way to go about that? Also is there anything else stored there automatically that I should be aware of, maybe something I wouldn’t necessarily want to delete.
Thank you!
Kevin
I think you can use this.
foreach (var directory in Directory.GetDirectories(Application.persistentDataPath))
{
DirectoryInfo data_dir = new DirectoryInfo(directory);
data_dir.Delete(true);
}
foreach (var file in Directory.GetFiles(Application.persistentDataPath))
{
FileInfo file_info = new FileInfo(file);
file_info.Delete();
}
On my project I tried writing an editor script to delete the contents of persistentDataPath
, like this:
DirectoryInfo dataDir = new DirectoryInfo(Application.persistentDataPath);
dataDir.Delete(true);
However, I got an IOException with a “Sharing violation” message. I think this is because another editor script has some file within persistentDataPath
open, and so it cannot be deleted.
So, I think the only way to properly delete the persistent data path is to close Unity3D and then use your operating system to delete the folder. On MacOS, this is ~/Library/Application\ Support/vendor/title/
and on Windows it is a similar path under your AppData\LocalLow
folder.
You can try to create a new file directory
Directory.CreateDirectory (Application.persistentDataPath + “FilesToDelete”);
Save the files you need to delete into the newly created Folder, then use
DirectoryInfo dataDir = new DirectoryInfo(Application.persistentDataPath + “/FilesToDelete”);
dataDir.Delete(true);
Hope this helps!