Sequelize: Destroy/Delete all records in the table

Mysqlnode.jssequelize.js

Mysql Problem Overview


I am using Mocha for Unit tests.

When testing begins, I would like to delete all the previous records in a table.

What I have tried:

db.User.destroy({ force: true }).then(() => {
}).then(() => done());


db.User.destroy(
    {where: undefined},
    {truncate: false}
).then(() => {
    return 
}).then(() => done());


db.User.destroy({}).then(() => {
    return db.User.bulkCreate(users)
}).then(() => done());

I keep getting the following error:

 Error: Missing where or truncate attribute in the options parameter of model.destroy.

How do I delete/destroy all the records in a table?

Mysql Solutions


Solution 1 - Mysql

You can try using

db.User.destroy({
  where: {},
  truncate: true
})

Solution 2 - Mysql

I was able to solve this problem with the code:

table.sync({ force: true });

This is a safer solution than the one proposed in maheshiv's answer.

Solution 3 - Mysql

It works for me: db.User.truncate()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1107173View Question on Stackoverflow
Solution 1 - MysqlmaheshivView Answer on Stackoverflow
Solution 2 - Mysqljokermt235View Answer on Stackoverflow
Solution 3 - MysqlМаксим КовальView Answer on Stackoverflow