sequelize / sequelize/sequelize

`Model.destroyMany` to destroy many instances in one query

Open
#15,459 2 comments 0 reactions 1 assignee View on GitHub

@ephys is already working on this.

Since Feb 1, 2024.

Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Feature Description

Describe the feature you'd like to see implemented

A new static model method, Model.destroyMany, which takes multiple instances and builds a single query that can be used to destroy multiple rows in one efficient query.

We already have Model.destroy, but it takes a where option, not an array of instances. This alternative is easy to build in user code, but I think having it out of the box would encourage the use of a more performant solution:

const users = User.findAll({ where: { inactive: true } });

await User.destroyMany(users);

Say users are users 1, 5, 6, 9, and 3, the above would produce the following SQL:

DELETE FROM "users" WHERE "id" IN (1, 5, 6, 9, 3)
Describe why you would like this feature to be added to Sequelize

See above

Is this feature dialect-specific?
  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s):
Would you be willing to resolve this issue by submitting a Pull Request?
  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in implementing my feature.

Indicate your interest in the addition of this feature by adding the 👍 reaction. Comments such as "+1" will be removed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.