Back to Sails

`.destroyOne()`

docs/reference/waterline/models/destroyOne.md

12.12.20001.9 KB
Original Source

.destroyOne()

Destroy the record in your database that matches the given criteria, if it exists.

usage
var destroyedRecord = await Something.destroyOne(criteria);

Before attempting to modify the database, Waterline will check to see if more than one record matches the given criteria. If so, it will throw an error instead of proceeding.

Usage

ArgumentTypeDetails
1criteria((dictionary))The Waterline criteria to use for matching the record in the database.
Result
TypeDescription
((dictionary?))Since .destroyOne() never destroys more than one record, if a record is destroyed then it is always provided as a result. Otherwise, undefined is returned.
Errors

See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.

Example

javascript
var burnedBook = await User.destroyOne({id: 4})
if (burnedBook) {
  sails.log('Deleted book with `id: 4`.');
} else {
  sails.log('The database does not have a book with `id: 4`.');
}

Notes

  • Because it always returns the destroyed record, if one was matched, this method does not support .fetch().
  • This method can be used with await, promise chaining, or traditional Node callbacks.
<docmeta name="displayName" value=".destroyOne()"> <docmeta name="pageType" value="method">