Delete many records in one query

Hi guys

I try to delete multiples records in one query on my app but nothing seems to work properly because the two arguments of the delete mutation (data and filter) does not seems to support array of Ids.

Is the there a way to do that properly in one query ?

Same thing on nested data, is the there a way to delete for example an user and all this posts in one query ?

Thank you in advance !

1 Like

Hi @rmonell!

So the data argument only accepts { id: ID!, force: Boolean } as arguments whereas the filter argument would allow for a field that’s mandatory and unique to be used.

There is not currently a deleteMany mutation, however there are a few ways you could accomplish it.

First is by adding multiple delete operations in a single request using aliases. For example:

mutation {
  user1: userDelete(data: { id: "id1" }) { success }
  user2: userDelete(data: { id: "id2" }) { success }
  user3: userDelete(data: { id: "id3" }) { success }
  user4: userDelete(data: { id: "id4" }) { success }
  ...etc
}

Depending on your data model, you might have it where the records you want to delete are dependent records, meaning that they have a mandatory belongs to relationship.

For example, a data model where Authors have many Posts, and every post must belong to and author.

In this case you could run a reconnect and it would drop and delete all of the Authors posts.

mutation {
  authorUpdate(data: {
    id: "authorID"
    posts: {
      reconnect: []
    }
  }) 
  {
    id
  }
}

Hope this helps!

1 Like

Hello @sebastian.scholl

Thank you very much for your help !
It helped me to better understand Delete mutations :slightly_smiling_face:

1 Like

We recently made an update to the API with a DeleteByFilter and DestroyByFilter mutation(s).

Docs here! https://docs.8base.com/docs/8base-console/graphql-api/mutations/#auto-generated-mutations