Edit Page

Destroy (blueprint)

Delete the record specified by id from the database forever and notify subscribed sockets.

DELETE /:model/:id

This destroys the record that matches the id parameter and responds with a JSON dictionary representing the destroyed instance. If no model instance exists matching the specified id, a 404 is returned.

Additionally, a destroy event will be published to all sockets subscribed to the record room, and all sockets currently subscribed to the record will be unsubscribed from it.

Parameters

#
Parameter Type Details
model String The identity of the containing model.

e.g. 'purchase' (in /purchase/7)
id
(required)
String The primary key value of the record to destroy, specified in the path.
e.g. '7' (in /purchase/7) .

Example

#

Delete Pinkie Pie:

DELETE /user/4

Run in Postman

Expected response
#
{
  "name": "Pinkie Pie",
  "hobby": "kickin",
  "id": 4,
  "createdAt": 1485550644076,
  "updatedAt": 1485550644076
}

Socket notifications

#

If you have WebSockets enabled for your app, then every client subscribed to the destroyed record will receive a notification where the event name is that of the model identity (e.g. user), and the “message” has the following format:

verb: 'destroyed',
id: <the record primary key>,
previous: <a dictionary of the attribute values of the destroyed record (including populated associations)>

For instance, continuing the example above, all clients subscribed to User #4 (except for the client making the request) might receive the following message:

id: 4,
verb: 'destroyed',
previous: {
  name: 'Pinkie Pie',
  hobby: 'kickin',
  createdAt: 1485550644076,
  updatedAt: 1485550644076
}

If the destroyed record had any links to other records, there might be some additional notifications:

Assuming the record being destroyed in our example had an association with a via, then either updated or removedFrom notifications would also be sent to any clients who are subscribed to those child records on the other side of the relationship. See Blueprints > remove from and Blueprints > update for more info about the structure of those notifications.

If the association pointed at by the via is plural (e.g. cashiers), then the removedFrom notification will be sent. Otherwise, if the via points at a singular association (e.g. cashier) then the updated notification will be sent.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference