Delete Directory

I’m currently trying to delete a directory, but somehow i can’t get it to work… am I doing something wrong here?

void UMyBPFunction::DeleteDirectory()
{
    IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	  FString Directory = FPlatformProcess::UserDir();
	  FString Folder = "\\Files\\";
	  FString CompletePath = Directory + Folder;

	  if (PlatformFile.DirectoryExists(*CompletePath))
	  {
	       PlatformFile.DeleteDirectory(*CompletePath);
	  } 
}

At first it wasn’t working. But after a few compilations and tries, it started to work just fine. (windows 10)

Are you using logs to check what is the CompletePath and whether not the condition is met?

So the code above worked for you right? I create a new directory almost the same way and it works fine but I just can’t delete the directory after I’m done with it. Could be that the directory is not empty and that’s why the code I provided doesn’t work? Should the directory be empty?

Yes, it must be empty.

I believe the DeleteDirectoy in FFileManagerGeneric (https://docs.unrealengine.com/en-US/API/Runtime/Core/HAL/FFileManagerGeneric/DeleteDirectory/index.html) let’s you specify whether or not to recursively delete a directory, but I never tested it.

So, I just tested and the DeleteDirectoy in FFileManagerGeneric allows you to recursively delete a directory of all its contents, so be careful:

	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	
	FString Directory = FPlatformProcess::UserDir();
	FString Folder = "\\Files\\";
	FString CompletePath = Directory + Folder;

	if (PlatformFile.DirectoryExists(*CompletePath))
	{
		//PlatformFile.DeleteDirectory(*CompletePath);
		FFileManagerGeneric::Get().DeleteDirectory(*CompletePath, true, true);
	}
1 Like

yep, I tested the DeleteDirectory function on an empty folder and it just works fine and I figured it out that the directory is not empty and that was the reason it wasn’t working. Forgot to update this question. Thank you for your time!