Delete Your Table Data by ID using Sequelize

in this Sequelize tutorial, We’ll learn how to delete table data by the ID column using Sequelize. I have already shared How To Add multiple rows using Sequelize bulkCreate() method.

Sequelize has inbuilt a Model.destroy() method that you can use to delete rows of data from your database table.

The destroy() method requires the where parameter to identify which row you want to delete from the table.

Let’s take the employee table which has the following table:

You can define Employee Models corresponding to the above table:

in the above sequelize model, I have defined id column as a primary Key.

Delete Row by Id in Sequelize

You can call destroy() from the Employee model to delete a row by its id value:

The code will return 1, the number of row(s) deleted from the table.

Delete row By Column name

The example below shows how to delete a row from a found instance using the findOne() method:

And that’s how you can delete row(s) from the SQL table with Sequelize.

Originally published at https://www.js-tutorials.com on October 16, 2022.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Parvez Alam

Hey, I am Parvez Alam. A software developer since 2009. I love learning and sharing knowledge. https://www.phpflow.com/