View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

.publishDestroy( {id}, [request], [options] )


Publish the destruction of a model

Description Accepted Data Types Required ?
1 ID of Destroyed Record int, string Yes
2 Request request object No
3 Additional options object No

publishDestroy() emits a socket message using the model identity as the event name. The message is broadcast to all sockets subscribed to the model instance via the .subscribe model method.

The socket message is an object with the following properties:

  • id - the id attribute of the model instance
  • verb - "destroyed" (a string)
  • previous - an object--if present, contains the attributes and values of the object that was destroyed.


If this argument is included then the socket attached to that request will not receive the notification.


If this is set, it is expected to be a representation of the model before it was destroyed. This may be used to send out additional notifications to associated records.

Example Usage


module.exports = {

  testSocket: function(req,res){

        var nameSent = req.param('name');

        if (nameSent && req.isSocket){

          User.findOne({name:nameSent}).exec(function findIt(err,foundHim){
            User.destroy({}).exec(function destroy(err){

        } else if (req.isSocket){

        console.log('User with socket id '' is now subscribed to all of the model instances in \'users\'.');

        } else {




    // Don't forget to handle your errors


<script type="text/javascript">
window.onload = function subscribeAndListen(){
    // When the document loads, send a request to users.testSocket
    // The controller code will subscribe you to all of the 'users' model instances (records)

    // Listen for the event called 'message' emited by the publishDestroy() method.
      if (obj.verb == 'destroyed') {
        console.log('User '' has been destroyed .');

function destroy(){

    // Send the name to the testSocket action on the 'Users' contoller

<div class="addButton" onClick="destroy()">Click Me to destroy user 'Walter' ! </div>


Any string arguments passed must be the ID of the record.

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-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.


We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book