DEV Community

Matan Shidlov
Matan Shidlov

Posted on

How to Fix the “Record to Delete Does Not Exist” Error in Prisma

When you use Prisma to interact with your database, you might run into an error that says:

An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.

In plain English, this means Prisma can’t find the record you’re trying to delete because your where clause does not match a unique key. Below, we’ll walk through how this happens and the steps to fix it, using a blog post example model.


Why This Error Occurs

In Prisma, when you delete a record like so:

await prisma.post.delete({
  where: { /* Some criteria */ },
});
Enter fullscreen mode Exit fullscreen mode

the where clause must reference a unique key—e.g., a primary key (@id), a field marked with @unique, or a group of fields defined with @@unique([...]) or @@id([...]) (a composite key).

If you include fields that aren’t guaranteed to be unique, Prisma won’t find (or won’t allow) the record because it doesn’t match the unique constraint, triggering the “Record to delete does not exist” error.


Example Scenario

Let’s say you have this model in your schema.prisma file:

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String
  published Boolean   @default(false)
}
Enter fullscreen mode Exit fullscreen mode

Here, id is the only uniquely identified field by default (the primary key).

1. Deleting by Primary Key Alone

Because id is unique, you can safely delete by id alone:

async function deletePost(id: number) {
  return prisma.post.delete({
    where: { id },
  });
}
Enter fullscreen mode Exit fullscreen mode

This works perfectly if all you need is the id to identify which Post to delete.


2. Checking Other Fields Before Deleting

What if you need to ensure that the post’s title matches before deleting it? Since title is not unique in the schema, you can’t just do:

// This might fail because (id, title) isn't a unique combination in the schema
await prisma.post.delete({
  where: {
    id,       // valid unique field
    title,    // not unique
  },
});
Enter fullscreen mode Exit fullscreen mode

Instead, you can:

  1. Find the record first by id and title (without requiring it to be unique).
  2. Throw an error if it doesn’t match.
  3. Delete it by the primary key (id).
async function deletePostByIdAndTitle(id: number, title: string) {
  // 1. Find the post by `id` and `title`
  const post = await prisma.post.findFirst({
    where: { id, title },
  });
  if (!post) {
    throw new Error('No post found with that ID and title');
  }

  // 2. Delete by the primary key
  return prisma.post.delete({
    where: { id: post.id },
  });
}
Enter fullscreen mode Exit fullscreen mode

This sequence ensures that you only delete a post if both id and title match the record you expect.


3. Using a Composite Unique Key

If your business logic requires (id, title) to be a unique pair, you can define a composite constraint in the schema:

model Post {
  id        Int      @default(autoincrement())
  title     String
  content   String
  published Boolean   @default(false)

  @@unique([id, title]) // or @@id([id, title]) if you want them as a composite primary key
}
Enter fullscreen mode Exit fullscreen mode

Now, Prisma recognizes (id, title) as a unique combination. You can then do:

await prisma.post.delete({
  where: {
    id_title: {
      id,
      title,
    },
  },
});
Enter fullscreen mode Exit fullscreen mode

(The exact syntax—id_title: { id, title }—comes from how Prisma interprets composite keys in your schema.)


Conclusion

To avoid the “Record to delete does not exist” error:

  1. Delete by a unique field (e.g., id) if that’s sufficient.
  2. Check first, then delete if you need to match non-unique fields.
  3. Define composite unique keys if multiple fields must uniquely identify your record.

By aligning your where clause with a valid unique or primary key—either by using existing unique fields or by creating a composite constraint—you’ll ensure your Prisma .delete() operations work without error.

Top comments (0)