How to save uasset from Plugin properly so that it can be edited/deleted in Editor

I am trying to develop a plugin. At some point, the plugin has to save material function uassets.

One of the material function (topFunction) uses other material material functions (subFunctions). When I click the button, the plugin saves all of them.

After they are saved, I cannot edit&save nor delete top function manually. However I can edit&save or delete sub functions manually (deleting topFunction in the first place or in the last place does not change the outcome). All functions show memory references as a warning when I tried to delete them. I can edit&save or delete topFunction only when I restarted UE4.

What am I doing wrong? How to save them from the plugin, so that no memory reference will interfere with manual operation? Is there any way to see the memory reference?

Top Function Material Code:

FString pluginName("PluginName");
FString projectPluginsDir = FPaths::ProjectPluginsDir();
FString projectPluginDir = FString::Printf(TEXT("%s%s/"), *projectPluginsDir, *pluginName);
FString projectPluginContentDir = FString::Printf(TEXT("%sContent/"), *projectPluginDir);

FString projectContentDir = FPaths::ProjectContentDir();
FString projectContentPluginDir = FString::Printf(TEXT("%s%s/"), *projectContentDir, *pluginName);
FPackageName::RegisterMountPoint(*FString::Printf(TEXT("/Game/%s/"), *pluginName), *projectContentPluginDir);

FString assetName = FString::Printf(TEXT("MF_Top"));
FString packageName = FString::Printf(TEXT("/Game/PluginName/%s"), *assetName);
UPackage* package = CreatePackage(nullptr, *packageName);

UMaterialFunction* topFunction = NewObject<UMaterialFunction>(package, UMaterialFunction::StaticClass(), *assetName, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone);
FAssetRegistryModule::AssetCreated(topFunction);

std::vector<UMaterialExpressionMaterialFunctionCall*> subFunctionExpressions;
for (unsigned int i = 0; i < count; i++) {
	UMaterialExpressionMaterialFunctionCall* subFunctionExpression  = NewObject<UMaterialExpressionMaterialFunctionCall>(
		topFunction, UMaterialExpressionMaterialFunctionCall::StaticClass(), *subAssetName, 
		EObjectFlags::RF_Public | EObjectFlags::RF_Standalone);
	subFunctionExpression->SetMaterialFunction(materialFunctions.at(i));
	subFunctionExpression->UpdateFromFunctionResource();
	subFunctionExpressions.push_back(subFunctionExpression);
	topFunction->DependentFunctionExpressionCandidates.Add(subFunctionExpression);
	topFunction->FunctionExpressions.Add(subFunctionExpression);
}

FString FilePath = FString::Printf(TEXT("%s%s%s"), *projectContentPluginDir, *assetName, *FPackageName::GetAssetPackageExtension());
bool bSuccess = UPackage::SavePackage(package, topFunction, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *FilePath, GLog, nullptr, false, false, SAVE_NoError);

I think that I had to ask to be able to find the solution on my own. For anyone seeking answer to a similar problem in the future:
Do not NewObject your UMaterialExpressionMaterialFunctionCall’s with EObjectFlags::RF_Standalone flag.

I can delete all of them now. But when I try to save any of them, it fails and says “cannot be saved as it has only been partially loaded”

However, although sub functions memory references are now gone, the top function still sees a memory reference

You have to add your packages like this and unload them all once you are finished with them.

TArray Packages;
Packages.Add(package);
UPackageTools::UnloadPackages(Packages);