Destroy records in your database that match the given criteria.
Something.destroy(criteria).exec(function (err) {
});
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! |
Argument | Type | Details | |
---|---|---|---|
1 | err | The error that occurred, or null if there were no errors. |
|
2 | deletedRecords | An array containing any records which were deleted. |
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();
});
- 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.