How to: Delete Elements from the Model

You can delete elements programmatically. As with all changes to the store, you must perform this action in a transaction.

This action has the same effect as if a language user had deleted the element from the design surface. The same events and rules are fired as if a language user had deleted the element.

By default, any embedded elements are also deleted because the deletion action is propagated to elements that are connected to the deleted element through an embedding relationship.

Note Note

A deletion is propagated through links where the PropagatesDelete flag is set on the role at the opposite end of the link. By default, this flag is set for embedding relationships that you create by using the Domain-Specific Language Designer. However, the flag would not be set if you created your .dsl file outside the designer.

To delete an element or link

  1. Create a transaction.

  2. Access the element or link that you want to delete (by using the ElementDirectory member of the store, for example).

  3. Call the Delete method for that element or link.

  4. Close the transaction.

In the following example, you create two elements, and then you delete one element in a transaction.

In this example, an element is created and deleted. Typically, you would find an existing element or link in the store using a Find or Get method of the store ElementDirectory member and then delete the element. For more information, see How to: Find Elements in the Store.

You use the Delete method for an element or link to delete that object.

using (Transaction txDeleteElem =
    store.TransactionManager.BeginTransaction("Delete element"))
        Library library = new Library(store);
        Library someelement = new Library(library.Partition);

    ReadOnlyCollection<ModelElement> foundelements = 
    int numlibraries = foundelements.Count;  // numlibraries = 1