Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Delete a batch of docs using admin SDK and callable cloud function in most similar way to client SDK?

I have an array of docs ids that I want to delete in using a cloud function, my code looks like the following :

//If the user decieds on deleting his account forever we need to make sure we wont have any thing left inside of db after this !!

// incoming payload array of 3 docs
data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']}

export const deleteClients = functions.https.onCall(async (data, context) => {
  try {
    // declare batch
    const batch = db.batch();

    // set
    data.arr.forEach((doc: string) => {
      batch.delete(db.collection('Data'), doc);
    });

    // commit 
    await batch.commit();
    
  } catch (e) {
    console.log(e);
  }
  return null;
});
I am getting a syntax error on batch.delete how to pass the right arguments in to the batch delete to reference that doc I want to submit for deletion before commit ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Delete takes a single param, the doc ref of the doc to be deleted.

data.arr.forEach((docId: string) => {
  batch.delete(doc(db, "Data", docId));
});
about 4 years ago · Juan Pablo Isaza Report

0

There are several errors in your code:

  • data.arr.forEach() cannot work wince your data object contains one element with the key array and not the key arr.
  • You are mixing up the syntax of the JS SDK v9 and the Admin SDK. See the write batch Admin SDK syntax here.
  • You need to send back some data to the client when all the asynchronous work is complete, to correctly terminate your CF.
  • You do return null; AFTER the try/catch block: this means that, in most of the cases, your Cloud Function will be terminated before asynchronous work is complete (see the link above)

So the following should do the trick (untested):

const db = admin.firestore();

const data = {array : ['a302-5e9c8ae97b3b','92c8d309-090d','a302-5e932c8ae97b3b']};

export const deleteClients = functions.https.onCall(async (data, context) => {
  try {
    
    const batch = db.batch();
    const parentCollection = db.collection('Data')
   
    data.array.forEach((docId) => {   
      batch.delete(parentCollection.doc(docId));
    });

    // commit 
    await batch.commit();
    
    return {result: 'success'} // IMPORTANT, see explanations above
    
  } catch (e) {
    console.log(e);
    // IMPORTANT See https://firebase.google.com/docs/functions/callable#handle_errors
  }
  
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!