Home > front end >  Eliminating documents from Firebase Realtime database (Not the entire collection)
Eliminating documents from Firebase Realtime database (Not the entire collection)

Time:08-25

I have deployed an app with React and I am using Firebase Realtime database to store some info about attention tickets in a call center. The database will store aprox 80 tickets info per day, but this is cumulative. I want to avoid this so I will not reach the firebase storage limit.

My idea so far is to delete every day at noon the tickets from the database, so It will only store the data from the current date and eliminate it at noon.

I am using remove() function from firebase, but when I tried referencing to the collection, It was entired deleted, I just want to delete the documents but not the entire collection.

Is there a way to specify Firebase to delete docs only, maybe to delete every docs except one?

This is the bunch of code that I pretend to use for deleting (Based on JS)

function deletedata(){
    const dbRef = query(ref(db,'/mycollectionpath'));
    onValue(dbRef, (snapshot)=>{
        
        snapshot.forEach(childSnapshot=>{
            let keyName = childSnapshot.key;
            remove(dbRef);
            
        })
        
        
    });
}
setInterval(deletedata,1000)

CodePudding user response:

The Firebase Realtime Database automatically creates parent nodes when you write a value under them, and it automatically deletes parent nodes when there are no longer any values under them. There is no way to have a node without a value.

If you want to keep the node, consider writing a dummy value. For example, this would remove all child nodes from mycollectionpath and replace them with a single true value:

const dbRef = ref(db, '/mycollectionpath');
dbRef.set(true);
  • Related