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

247
Vistas
how can i delete multiple paths with firebase cloud functions

I want to delete data older than 15 days. Can I query and delete multiple directories at the same time?

The database structure is like this. There are multiple languages and types.

{
  "hr" : {
    "classic" : {
      "finish" : {
        "-MuJD4oAlQ42TkdfKW5M" : {
          "gid" : "LlAHMyj",
          "ts" : 6.652890289073215E8
        }
      }
    },
    "duel" : {
      "finish" : {
        "-MuJD4o9LONeayt4ACMC" : {
          "gid" : "rfePU2x",
          "ts" : 6.652890289073215E8
        }
      }
    },
    "target" : {
      "finish" : {
        "-MuJD4o9LONeayt4ACMD" : {
          "gid" : "FBz4H_4",
          "ts" : 6.652890289073215E8
        }
      }
    }
  },
  "nl" : {
    "target" : {
      "finish" : {
        "-MuJD4oAlQ42TkdfKW5L" : {
          "gid" : "sv3pRfC",
          "ts" : 6.652890289073215E8
        }
      }
    }
  }
}

I tried to do it with the code below but it always returns null.

exports.deleteOldData = function (snap, context) {
  const end = new Date(new Date().setDate(new Date().getDate() - 15));
  const endDate = toSwiftDate(end);

  database.ref("/matchTour/{language}/{type}/finish/").orderByChild("ts").endAt(endDate).transaction(function (info) {
    if (info === null) return info;
    console.log("matchTour 3", JSON.stringify(info.val()));
    return null;
  }).catch((error) => {
    console.log("error);
  });
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't run a transaction on a query. In fact, from looking at the reference documentation for Query this shouldn't even compile.

This should be closer:

exports.deleteOldData = function (snap, context) {
  const end = new Date(new Date().setDate(new Date().getDate() - 15));
  const endDate = toSwiftDate(end);

  const ref = database.ref("/matchTour/{language}/{type}/finish/");
  return ref
    .orderByChild("ts").endAt(endDate)
    .once("value")
    .then((results) => {
    let updates = {};
    results.forEach((snapshot) => {
      updates[snapshot.key] = null;
    });
    return ref.update(updates);
  }).catch((error) => {
    console.error(error);
  });
};

The above reads the results for the query from the data, performs a single multi-path update to delete all of them, and ensures the the promises returned by the asynchronous read and write bubble up, so that your Functions container doesn't get terminated prematurely.


That leaves the question of what language and type are in database.ref("/matchTour/{language}/{type}/finish/"). If there are meant to be variables that come from the context, you'll want to use backticks for this string:

database.ref(`/matchTour/{language}/{type}/finish/`)

All in all there are quite a few mistakes in this code that make me think you may not yet be too comfortable with writing server-side JavaScript. If that is the case, writing Cloud Functions is not the easiest way to get started, and I recommend first writing the same code in a client-side JavaScript environment, like a local node.js script, or even a web page.

Reading the Firebase documentation for Web developers and/or taking the Firebase codelab for Web developer are great starting points.

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