Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda