How I could delete multiple items in one API call?

I want to delete multiple items in one API call, something like the following:

mutation{
delete_item(item_id: 5406008582) {
id
}
delete_item(item_id: 5406008601) {
id
}
}

You need to use multiple named mutations (instead of anonymous mutations) in the same request:

mutation delete1 {
  delete_item(item_id: "12321321") {
    id
  }
}

mutation delete2 {
  delete_item(item_id: "5242344") {
    id
  }
}

Notice how each mutation has a name (delete1, delete2) now? this allows multiple mutations in the same request.

2 Likes

Thanks @codyfrisch. It worked.

1 Like

A few things to watch:

Since the API server has a limit of 5000 connections per IP per minute:

If this is a UI based feature, it may be preferable to just do two queries, and use Promise.allSettled() to execute concurrently but handle them individually.

If you’re using a microservice backend, I would do the same since the IP space your microservices run in is much larger. You won’t run into limits by having concurrent connections.

If you’re using a monolithic server (container or VM) that is serving many accounts simultaneously, from a single IP, then using GraphQL batching as shown may be a way avoid exhausting the 5000 cpm rate limit in a busy environment.

The problem with batching the mutations into a single call, is with error handling and retrying since you now must parse responses, figure out which ones failed, and retry them as appropriate. This particularly comes in with API complexity limits. Individual requests will error individually and you don’t need to write code just to figure out what failed - you already know.

2 Likes

@codyfrisch
The code you have shared is working fine when I execute it on https://monday.com/developers/v2/try-it-yourself, but when I convert it to Python it no longer works.

query = '''
mutation delete1($item_id_1: Int!) {
        delete_item(item_id: $item_id_1) {
                id
        }
}

mutation delete2($item_id_2: Int!) {
        delete_item(item_id: $item_id_2) {
                id
        }
}
'''
variables = {'item_id_1': 5406008, 'item_id_2': 548754}
data = {'query' : query, 'variables' : variables}
response = requests.post(url=apiUrl, json=data, headers=headers) # make request
response.json()

The code above give me the following error:

{'errors': [{'message': 'An operation name is required'}], 'account_id': 196}

The query you shared works fine on https://monday.com/developers/v2/try-it-yourself but the exact same query in Python give me the same error I shared above.

1 Like

Hello there @AmirSaleem,

Can you please try something like this?

mutation($item_id_1: Int!, $item_id_2: Int!){ 
    delete1: delete_item(item_id: $item_id_1) {id}

    delete2: delete_item(item_id: $item_id_2) {id}
}

Let me know how that goes!

Cheers,
Matias

2 Likes

It worked. Thanks @Matias.Monday

I’m glad that worked @AmirSaleem :grin:

1 Like