Search code examples
javascriptprisma

Prisma deleteMany with a list of IDs


I'm wondering if there is a way in the Prisma Client to batch delete database records by id.

Something like this doesn't seem to exist:

const idsToDelete = [5, 29, 255]

prisma.post.deleteMany({
    where: {
        id: {
            equals: idsToDelete
        }
    }
})

The docs allude to the concept of Scalar List Filters, but this doesn't seem to be supported for numeric lists or perhaps isn't supported in deleteMany.

Under the hood, I'm hoping for a SQL DELETE ... WHERE IN clause. I'd prefer not to:

  • Spin up a bunch of individual JS promises
  • Use database-specific Prisma features (ok if it's not supported in MongoDB)
  • Write SQL directly

Solution

  • You can use the in operator:

        where: {
            id: {
                in: idsToDelete
            }
        }