How to delete an asset from Editor in C++?

Hello,

I wanna delete a bunch of assets inside the Editor (content browser) with C++ code. I already tried calling:

UObject* object = ... ;
object->ConditionalBeginDestroy();
GEditor->ForceGarbageCollection(true);

but that leaves an empty asset behind (called ‘NONE’).
Any ideas how i could do this properly?

Took me some time to figure it out, but i think you can not instantly destroy/ delete Assets (or any subclass of UObject). Instead you can mark it as “pending kill” and the garbage collector will pick it up eventually. I did it by simply calling this function:

object->MarkPendingKill();

Additional hint:

forcing the garbage collector to do its work by calling ForceGarbageCollection(true) apparently is not a good idea, as it caused the editor to crash, due to some assets being a “nullptr”.

And then, u need use

FAssetRegistryModule::AssetDeleted( AssetData.GetAsset() ) ;

to delete the Asset !

ObjectTools::DeleteAssets(AssetsToDelete);