Deleting file through code not working?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

For some reason, I can’t delete files from the user:// directory in Godot Mono.

Directory d = new Directory();
File f = new File();

if(d.DirExists(path)) {
	GD.Print(Name, "Save file directory was found.");

	switch(f.FileExists(path + filename)) {
		case true:
			GD.Print(Name, "Save file found. Deleting...");
			d.Remove(path + filename);
			GD.Print(Name, "Save file was deleted!");
			break;
		case false:
			GD.Print(Name, "Error. Save file doesn't Exist.");
			break;
	}
} else {
	GD.Print(Name, "Error. Save file directory not found.");
}

It detects the directory and the file, but the file remains after using Remove(). Am I skipping something?

From the console window:

[11:31:26] [Load]: Save file directory was found.
[11:31:26] [Load]: Save file found. Deleting…
[11:31:26] [Load]: Save file was deleted!

:bust_in_silhouette: Reply From: 9BitStrider

I found the issue. I had recently opened the same file in another function but forgot to close it. Problem solved.

:bust_in_silhouette: Reply From: zhyrin

Directory.Remove() should be returning an error code, at least checking if it’s OK is useful to save you headache :slight_smile: