Skip to content

Prisma, how to clear the database

While testing a site that used Prisma I had the need to clear the database from time to time, to clear the test data I entered.

You can clear items entered by using:

await prisma.user.deleteMany({})

If for some reason you want to iterate on the items to do some processing, you can iterate over them in this way:

const users = await prisma.user.findMany({})

const deleteUser = async (user) => {
  return await prisma.user.delete({
    where: { id: user.id }
  })
}

const deleteUsers = async () => {
  users.map((user) => deleteUser(user))
}

deleteUsers()

In this case I am not doing anything more than the previous example, which makes all this code redundant, but you could do anything you want inside deleteUser().

I had a problem though because I had a relation between 2 tables, tweets and users. A tweet was associated to a user. First I had to remove all tweets, then remove all users, so I wrote this function:

export const clearData = async (prisma) => {
  const users = await prisma.user.findMany({})
  const tweets = await prisma.tweet.findMany({})

  const deleteUser = async (user) => {
    return await prisma.user.delete({
      where: { id: user.id }
    })
  }
  const deleteTweet = async (tweet) => {
    return await prisma.tweet.delete({
      where: { id: tweet.id }
    })
  }

  const deleteTweets = async () => {
    return Promise.all(tweets.map((tweet) => deleteTweet(tweet)))
  }

  const deleteUsers = async () => {
    return Promise.all(users.map((user) => deleteUser(user)))
  }

  await deleteTweets()
  await deleteUsers()
}

Notice the use of Promise.all() to wrap users.map() so I could use await on it, so all tweets are removed before I start deleting users.


download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about database: