C# example to delete content in editor

Currently I am deleting unwanted content by using File.Delete(file); from System.IO, but this feels bad because it effectively deletes the object outside of Flax with no nice way to check if the content is in use / remove it from the Flax content database. Is there a better way to remove unused content?

Example, suppose I create a new material in /Content/MyProject/Materials, but I later want to remove all materials from this directory. What is the recommended way to do this using C#?

FlaxEditor.Editor.Instance.ContentDatabase.Delete(); will get you what you are wanting. It only takes in ContentItems, so you will have to do FlaxEditor.Editor.Instance.ContentDatabase.Find(); to get the content items you are wanting to remove.

1 Like

Thanks Tryibion, I tried using:

ContentFolder materialsFolder = (ContentFolder)Editor.Instance.ContentDatabase.Find("MyMaterials");

Where “MyMaterials” is a folder: MyProject/Content/MyProject/MyMaterials, but materialsFolder always returns as null. Any thoughts? I’m not sure what the expected full path is, e.g. should I be using:

.Find(“Content/MyProject/MyMaterials”)

or

.Find(“MyProject/MyMaterials”)

or…

Thanks, sorry for my fumbling around!

Something like this will work
var folder = Editor.ContentDatabase.Find(Path.Combine(Globals.ProjectContentFolder, "<your folder>")) as ContentFolder;

1 Like

Thanks so much for your help! All working now.

For future reference I found I needed to add an Instance to the line to give

ContentFolder materialsFolder = Editor.Instance.ContentDatabase.Find(Path.Combine(Globals.ProjectContentFolder, folder)) as ContentFolder;