Models
Deleting Models
Deleting models with Vasta.
Delete
To delete a model from the database, call the delete method on a model instance. This will remove the record from the database.
const pet = await Pet.findOrFail(1);
await pet.delete();
Destroy
If you already have the model's primary key but haven't retrieved it, you can delete the model using the async destroy method. This resolves and returns the number of deleted models
const deleted = await Pet.destroy(1);
// deleted is 1 if the model with primary key 1 was deleted
You can also delete multiple models at a time by passing an array.
const deleted = await Pet.destroy([1, 2, 3]);
// deleted is 3 if three records were deleted
Events
When you delete a model, Vasta dispatches two events:
deleting: Dispatched immediately before the record is deleted from the database.deleted: Dispatched immediately after the record is successfully deleted.
You can learn more about hooking into these events in the Events documentation.