.destroy()
Destroy records in your database that match the given criteria.
await Something.destroy(criteria);
or
var destroyedRecords = await Something.destroy(criteria).fetch();
Argument | Type | Details | |
---|---|---|---|
1 | criteria | Records matching this Waterline criteria will be destroyed. Be warned, if you specify an empty dictionary ({} ) as your criteria, all records will be destroyed! destroy queries do not support pagination using skip and limit or projections using select . |
Type | Description |
---|---|
The destroyed records are not provided as a result by default in order to optimize for performance. To override the default setting, chain .fetch() and the newly destroyed records will be sent back. (Be aware that this requires an extra database query in some adapters.) |
Name | Type | When? |
---|---|---|
UsageError | Thrown if something invalid was passed in. | |
AdapterError | Thrown if something went wrong in the database adapter. | |
Error | Thrown if anything else unexpected happens. |
See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.
Key | Type | Details |
---|---|---|
fetch | If set to true , then the array of destroyed records will be sent back.Defaults to false . |
For more information on meta keys, see .meta().
To delete any users named Finn from the database:
await User.destroy({name:'Finn'});
sails.log('Any users named Finn have now been deleted, if there were any.');
To delete two particular users who have been causing trouble:
await User.destroy({
id: { in: [ 3, 97 ] }
});
sails.log('The records for troublesome users (3 and 97) have been deleted, if they still existed.');
To delete a particular book and fetch the destroyed record, use .destroyOne().
To delete multiple books and fetch all destroyed records:
var burnedBooks = await Book.destroy({
controversiality: { '>': 0.9 }
}).fetch();
sails.log('Deleted books:', burnedBooks);
- This method can be used with
await
, promise chaining, or traditional Node callbacks.- If you want to confirm that one or more records exist before destroying them, you should first perform a
find()
. However, it is generally a good idea to try to do things rather than checking first, lest you end up with a race condition.