Delete Multiple Subitems in one request

Does anyone know if you can delete multiple subitems in one request. It’s fairly slow to do it through iteration as seen here:

export const clearSubitems = async (event: TEvent) => {
  const response = await monday.api(`{
    items(ids:[${event.pulseId}]) {
      subitems {
        id
      }
    }
  }`);

  const idsToDelete = response.data.items[0].subitems.map(
    (item: { id: string }) => item.id
  ) as string[];

  for (const id of idsToDelete) {
    await monday.api(`
      mutation {
        delete_item(item_id: ${id}) {
          id
        }
      }
    `);
  }
};

Thanks for the help!

export const clearSubitems = async (event: TEvent) => {
  const response = await monday.api(`{
    items(ids:[${event.pulseId}]) {
      subitems {
        id
      }
    }
  }`);

  const idsToDelete = response.data.items[0].subitems.map(
    (item: { id: string }) => item.id
  ) as string[];
  
const deletePromises = idsToDelete.map((id) => { 
  return monday.api(`
    mutation {
      delete_item(item_id: ${id}) {
        id
      }
    }
  `);
})

const results = await Promise.allSettled(deletePromises);
// iterate your results, its an array of objects with success (boolean), data, and 
// an error object if there is an error. 
  
};

Just don’t await each request, instead just get the promises, and then use await Promise.allSettled() to await them all. This will let the JS runtime execute all of the I/O portions on separate threads in the background, and then await all of them to return. While JS is single-threaded the IO that runs outside the JS runtime is not.

2 Likes