Skip to main content

Delete 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();
}

If the child element is owned, you can also delete it using the RemoveField method of the IModel object as follows. If it is a reference, simply delete the reference.

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 a scenario
uc.RemoveField("Scenarios", scenario);
c.App.Output.WriteLine("sample", $"Scenario IsDeleted: {scenario.IsDeleted}");
}

Note

When you use the IModel.RemoveField method on a reference field, it only deletes the reference relationship, not the related model.