Deleting a Model
To delete a model, call the Delete method of the IModel object.
public void DeleteModel(ICommandContext c, ICommandParams p)
{
IModel model = c.App.Workspace.CurrentModel;
//Delete the model
model.Delete();
}
Note
Models that have a base model for manually loaded model files under them cannot be deleted from extensions or scripts, resulting in an error. To delete them, either merge the model files under them or unregister the model files before executing. See Changes in V5.1 for details.
If the child element has an ownership relationship, it can also be removed using the RemoveField method of the IModel object, as shown below. For references, simply remove the reference relationship.
public void RemoveField(ICommandContext c, ICommandParams p)
{
// Create a model in the project
var ucModel = c.App.Workspace.CurrentProject.AddNewRootModel("UseCaseModel");
// Create a use case
IModel uc = ucModel.AddNewModel("UseCases", "UseCase");
// Add a scenario to the use case
IModel scenario = uc.AddNewModel("Scenarios", "Scenario");
c.App.Output.WriteLine("sample", $"Scenario: {scenario.Name}");
// Delete the scenario
uc.RemoveField("Scenarios", scenario);
c.App.Output.WriteLine("sample", $"Scenario IsDeleted: {scenario.IsDeleted});
}
Note
When the IModel.RemoveField method is used on a referenced field, only the reference association is removed; the associated model is not deleted.