View our GitHub

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

Edit Page

.destroy()

Destroy records in your database that match the given criteria.

Something.destroy(criteria).exec(function (err) {

});

Usage

Argument Type Details
1 criteria Records which match this Waterline criteria will be destroyed. Be warned, if you specify an empty dictionary ({}) as your criteria, all records will be destroyed!
Callback
Argument Type Details
1 err The error that occurred, or undefined if there were no errors.

Example

To delete any users named Finn from the database:

User.destroy({name:'Finn'}).exec(function (err){
  if (err) {
    return res.negotiate(err);
  }
  sails.log('Any users named Finn have now been deleted, if there were any.');
  return res.ok();
});

To delete a particular book which is no longer available:

Book.destroy({
  id: 4
}).exec(function (err){
  if (err) {
    return res.negotiate(err);
  }
  sails.log('Deleted book with `id: 4`, if it existed.');
  return res.ok();
});

To delete two particular users who have been causing trouble:

User.destroy({
  id: [ 3, 97 ]
}).exec(function (err){
  if (err) {
    return res.negotiate(err);
  }
  sails.log('The records for troublesome users (3 and 97) have been deleted, if they still existed.');
  return res.ok();
});

Notes

  • If you want to confirm that one or more records exist before destroying them, you should first perform a find(). However, keep in mind it is generally a good idea to try to do things rather than checking first, lest you end up with a race condition.

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.

Reference

We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book