docs / articles / Sequelize Row Removal

Sequelize Row Removal

Lisa Jul 26, 2026 2026-07-26 18:03:27

When working with databases, deleting a row of data is an inevitable task. In the context of Node.js, Sequelize, an Object Relational Mapping (ORM) library, provides a convenient way to carry out database operations, including deleting rows. This article delves into the intricacies of deleting a row using Sequelize.

Delete Row vs Clear Contents in Excel
Delete Row vs Clear Contents in Excel

Sequelize offers multiple methods to remove rows from a table. Understanding these methods is crucial for efficient data management. Let's explore these methods, their use-cases, and best practices.

VBA Delete Rows | Examples to Delete Excel Rows using VBA
VBA Delete Rows | Examples to Delete Excel Rows using VBA

Destroy and Remove Methods

The 'destroy' and 'remove' methods in Sequelize are used to delete instances from the database. They are similar but with a slight difference in how they handle associations.

How to delete yourself from the internet
How to delete yourself from the internet

Both methods work by taking an ID, an instance, or an array of IDs/instances as parameters. They then remove the corresponding rows from the database.

Using IDs

How to Find and Delete Rows in Excel (5 Methods) - ExcelDemy
How to Find and Delete Rows in Excel (5 Methods) - ExcelDemy

The simplest way to delete a row is by providing the ID of the instance you wish to remove. Here's an example:

const instanceId = 1; YourModel.destroy({ where: { id: instanceId } });

In this example, replace 'YourModel' with the name of your Sequelize model.

Using Instances

Error deleting file web graphic from windows xp
Error deleting file web graphic from windows xp

If you have an instance of the model, you can also use the 'destroy' or 'remove' methods directly on the instance:

const instance = await YourModel.findOne({ where: { id: 1 } }); instance.destroy();

Or, with 'remove':

const instance = await YourModel.findOne({ where: { id: 1 } }); instance.remove();

Both of these will delete the instance from the database.

How to Delete Empty Rows at the Bottom in Excel? - ExcelDemy
How to Delete Empty Rows at the Bottom in Excel? - ExcelDemy

Deleting with Associations

When dealing with associations, you might want to delete an instance but keep its associated instances. This is where the difference between 'destroy' and 'remove' becomes significant.

a red delete button with the word delete on it in white letters against a black background
a red delete button with the word delete on it in white letters against a black background
How to Delete Selected Rows in Excel? - ExcelDemy
How to Delete Selected Rows in Excel? - ExcelDemy
a black computer key with the word delete on it's left hand side
a black computer key with the word delete on it's left hand side
a white computer keyboard with the word delete on it's left hand side
a white computer keyboard with the word delete on it's left hand side
the word delete is displayed on a white computer key with black letters in it
the word delete is displayed on a white computer key with black letters in it
入力したデータを削除する「BackSpace」「Delete」
入力したデータを削除する「BackSpace」「Delete」
How to Delete Rows in Excel with Specific Text: 3 Ways - ExcelDemy
How to Delete Rows in Excel with Specific Text: 3 Ways - ExcelDemy
done and dusted
done and dusted
an error screen with the text delete my feelings for you loading please wait on it
an error screen with the text delete my feelings for you loading please wait on it

Using 'destroy' will delete the instance and all its associated instances. On the other hand, 'remove' will only delete the instance, leaving its associations intact.

Destroying with Associations

If you want to delete an instance and all its associated instances, you can use the 'destroy' method with the 'force' option set to true:

const instance = await YourModel.findOne({ where: { id: 1 } }); await instance.destroy({ force: true });

This will delete the instance and all its associated instances.

Removing with Associations

If you want to delete an instance but keep its associated instances, use the 'remove' method:

const instance = await YourModel.findOne({ where: { id: 1 } }); await instance.remove();

This will delete only the instance, leaving its associations intact.

Understanding these methods and their use-cases can greatly simplify your database operations. Always remember to handle associations carefully to avoid unwanted data loss. With Sequelize, deleting rows in your database is a straightforward and secure operation.