You use the delete OCL/EAL operator in a ViewModel action or the OCL/PS ExecuteAndDebug tool to mark a persisted object for removal at the next save.
What delete does
delete marks its target object for deletion in the current ECO space. The object is permanently removed from persistence when the next save operation succeeds.
Until that save occurs, a previously obtained reference can still point to the object. Do not treat such a reference as a valid object to delete again.
Syntax
object.deleteThe target must be the object you want to remove.
Delete one object
Use self.delete when the action runs in the context of the object to remove.
self.deleteFor example, an action on a Customer ViewModel can delete the Customer represented by self. The Customer is marked for removal; it is not permanently removed until the next save.
Delete an object selected from a collection
You can navigate to a collection and delete an item from it.
Customer.allInstances->first.deleteThis expression deletes the first object returned by Customer.allInstances. Use this form only when selecting an arbitrary first Customer is appropriate for your action; it does not express a business-specific selection rule.
Delete every object in a collection
Use foreach when you need a side effect for every object in a collection.
self.Orders->foreach(o | o.delete)In this example, each Order associated with the current object is marked for deletion. foreach performs the deletion side effect and returns the original collection unchanged.
Handle relationships before deleting
A delete can fail when the target object, or objects removed through cascading deletion, still has associations that prevent deletion. Composite associations follow cascade-delete rules, but an association that is only aggregate can still block the operation when it is not empty.
Implement OnDelete to run logic when an object is deleted. When deletion is blocked by a reason that your model can resolve, use the special OnDeleteReasonSolve(reason:string; objectschecked:Collection(SysSuperClass)) method described on that page. It receives the blocking reason and the objects checked so far; it must resolve the complete blocking condition or the delete fails with the reported reason.
For example, where the appropriate model rule is to remove links that block deletion, the resolution logic can clear the relevant association before the framework checks deletion again. Design this behavior deliberately: clearing an association changes the object graph.
Avoid deleting an object twice
An object remains accessible through an earlier reference until the ECO space is saved, even after it has been marked for deletion. Calling delete again on that reference causes an "Object is already deleted" error.
Guard the operation with existing when the object may already have been marked for removal:
object.existing.whenTrue(object.delete)For details, see Documentation:Object is already deleted.
delete is not an HTTP DELETE request
delete removes an object from MDriven persistence. It does not call an external REST service.
To issue an HTTP DELETE request to a RESTful service, use RestDelete instead. See REST Delete for the HTTP operation.
