🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Rest Delete
This page was created by Stephanie on 2023-04-26. Last edited by Wikiadmin on 2026-07-29.

You can expose a REST service that accepts HTTP DELETE requests and use a ViewModel action to remove the targeted object; this page is for MDriven developers configuring that behavior.

What HTTP DELETE means

HTTP DELETE is the REST request method used to ask a server to remove the resource identified by the request URL.

In MDriven, allowing a ViewModel to receive REST requests does not by itself define what a DELETE request removes. You define the deletion behavior in the ViewModel, typically with an action whose OCL deletes the object selected by the REST request.

For example, a client can send a DELETE request for one exposed item. The ViewModel receives the request, identifies its target object, and an action can run self.delete on that object.

Before you implement DELETE

Prepare the ViewModel as a REST endpoint before adding deletion behavior.

  1. Create or select the ViewModel that represents the resource you want to expose.
  2. Set the ViewModel tagged value Eco.RestAllowed to True. This registers the ViewModel for REST operations, including GET, POST, PUT, PATCH, and DELETE.
  3. Ensure that the request is anchored to the object that is intended to be deleted. A DELETE operation must have a clear target; do not let an action delete an arbitrary object selected by unrelated input.
  4. Use the REST endpoint for the ViewModel. The base endpoint format is http://<host>/Rest/<ViewModelName>/. See Rest Services In MDriven for the request URL and object-identity format.

Delete the target object

Add an action to the root ViewModel class that operates on the object represented by the REST request. Use the delete operator in that action.

For a ViewModel whose root object is the item being deleted, the action expression is:

self.delete

delete marks the object for removal from persistence. The object is permanently removed after the next save operation.

Example: delete one exposed item

Assume that an Item object is exposed through a ViewModel named ItemRest.

  1. A client addresses one existing Item through the ItemRest REST URL and sends an HTTP DELETE request.
  2. The ViewModel resolves the request to that Item object.
  3. The delete action runs self.delete.
  4. When MDriven saves the change, the Item is removed from persistence.

Keep the delete action on the object that the request identifies. For example, do not use an expression such as Item.allInstances->first.delete for a public resource endpoint unless deleting that particular first object is explicitly the intended business rule.

Run deletion only for DELETE requests

A REST-exposed ViewModel can receive multiple HTTP verbs. Add a ViewModel variable that receives the REST verb, then use the action's enablement or read-only rules to ensure that the delete action is available only when the received verb is DELETE.

This lets one ViewModel support different behavior for different requests. For example:

Received REST verb Intended ViewModel behavior
GET Return the exposed data; do not run the delete action.
PATCH Apply the ViewModel's PATCH-specific update behavior; do not run the delete action.
DELETE Enable the delete action for the identified target object.

The REST verb can also be used to control other actions. For example, an action that creates related data can be enabled for POST while the deletion action is enabled only for DELETE. See the REST verbs walkthrough for an example of using ViewModel rules to distinguish verbs.

Design and safety considerations

  • Expose only the data and operations you intend to make available. Set Eco.RestAllowed only on ViewModels that are meant to be REST endpoints.
  • Delete the intended target. Make the REST request resolve to the resource object before the delete action runs.
  • Keep verb-specific rules explicit. A ViewModel can be called with GET, POST, PUT, PATCH, or DELETE. Do not rely on an action being harmless when it is reached through another verb.
  • Account for object relationships. Before deleting an object with associations, review what data must be loaded and how related objects are handled. The delete operator documentation notes that self.CascadeFetch can be considered before deletion to control fetching.
  • Model lifecycle rules when needed. If deletion depends on the object's business state, define that rule in the ViewModel logic and consider State Diagrams for lifecycle modeling.

Related REST operations

DELETE is one of the REST operations available to a REST-enabled ViewModel. Use the operation that matches the intent of the request:

Operation Use it when you want to More information
POST Send data and run POST-specific ViewModel actions. Documentation:Rest Post
PATCH Apply a partial update to exposed fields. Documentation:OCLOperators RestPatch
DELETE Run ViewModel logic that removes the identified object. This page and Documentation:OCLOperators delete

See also