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

202
Views
How to delete parent key by its value in firebase using javascript

So I'm a newbie in using firebase real time database and I can't delete products key child with id specified.

enter image description here

I've tried this approach but it didn't yield any success:

removeProductById = async (id, user) => {
    const userProductRef = db.ref(`products/product`);
    try{
        var query = userProductRef.orderByChild('id').equalTo(id);
        query.remove()             
        })

    }catch(error){
        console.log(`The error has occured ${error.message}`)
    }
}

So I wanna know how can one delete any tipe of key by whatever value they specify. Thank you in advance, you have always helped me guys.

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

0

There are two problems here:

  1. Your query doesn't match the nodes you want delete.
  2. Firebase doesn't support delete-queries.

Your query doesn't match the nodes you want delete

Your query starts at a node products/product, which doesn't exist in your database, and then orders/filters the id property of each child node.

What you want instead is to start at the products node, and then order/filter on its product/id property of each child node:

In code:

const userProductRef = db.ref(`products`);
var query = userProductRef.orderByChild('product/id').equalTo(id);

Don't forget that you'll need to define an index for product/id for this on the products level in your rules.

Firebase doesn't support delete-queries

You can't simply pass the query and delete instruction to Firebase. To write a node in the database, you'll need to know the entire path to that node.

So you'll need to:

  1. Execute the a query.
  2. Loop over the results in your application code.
  3. Delete each matching node individually or in a multi-path update.

With the query above, that'd be:

query.once("value").then((snapshot) => {
  snapshot.forEach((child) => {
    child.ref.remove();
  });
})

Or with a multi-path/batch update:

query.once("value").then((snapshot) => {
  let updates = {};
  snapshot.forEach((child) => {
    updates[child.key] = null;
  });
  ref.update(updates);
})
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!