DELETE Queries
This guide assumes you understand how to create models.
In the examples below, we will use the User
class, which is a Model.
Deleting a single row
You can delete an instance by calling the destroy
instance method on your model:
const jane = await User.create({ name: 'Jane' });
// jane is now in the database
await jane.destroy();
// Now this entry has been removed from the database
Deleting in bulk
Delete queries also accept the where
option, just like the read queries shown above.
// Delete everyone named "Jane"
await User.destroy({
where: {
firstName: 'Jane',
},
});
destroyAll
can be called on the sequelize instance to delete all data in the database.
This is useful if you want to reset the database state between tests.
await sequelize.destroyAll();
Truncating
Models also expose a truncate
method that will delete all rows in a table.
// Truncate the table
await User.truncate();
truncate
can also be called on the sequelize instance to delete all data in the database.
This is useful if you want to reset the database state between tests.
This operation is faster than calling destroyAll
, but may not work if you have foreign key constraints.
await sequelize.truncate();