Keeping Firebase Realtime Database clean

Hello thunkers !
Heres my today`s issue (ill simplify to basics): My app allows users to create some datas on Firebase.
screen
In my example, “FISHING TEST 2021” is created by a user. And this kind of datas will be created by any user that want to define his own contest.
My thought is to delete some of these contests after a while (lets say that i want to delete a contest 3 months after creation, because will become old and unnecesary datas will fill the server. (dont want to fill up the firebase with old datas not needed anymore).
My solution is to add a field on a new created data that keeps time of creation, then when any user login it checks the entire list and delete all old data (checking list is made anyway for other purposes).
And now my question: maybe theres a more efficient solution, maybe some Realtime database rules that will do this automatically or something like that ?
Thank you.

Hi, I would have the same need, to be able to automatically delete the data entered in the firebase after 48 hours from their insertion. it’s possible? Thanks for who can answer me.

1 Like

Usually a cloud function running on a chron j cycle would do this for you.

You could build an app that checks the date value for entries and deleted them accordingly BUT you’d have to do this manually.

Thanks for the reply, would you have some examples?

I don’t have any examples set up. These would have to be custom coded solutions if they were Firebase functions.

^^^ have seen this in action

You might be able to set up a service like Integromat/Xano using the REST api interface to check and delete old nodes in your firebase DB JSON

^^^ never seen this in action

You have two options:

  1. You add a timestamp along with your data so that you can use an app or a function in your app to clean your database by retrieving all values and loop through to check if any of the entries need to be deleted.

  2. Use Firebase functions to check the internal Firebase data and remove old entries.

Firebase keeps internal logs which has the timestamp of the created record time but you can only get these internal values through the Firebase Functions.

1 Like

Thanks for the tips.

1 Like

Welcome.

See the following example from Firebase docs to remove all entries that are older than 2 hours.

exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
.onWrite((change, context) => {
  var ref = change.after.ref.parent; // reference to the items
  var now = Date.now();
  var cutoff = now - 2 * 60 * 60 * 1000;
  var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);
  return oldItemsQuery.once('value', function(snapshot) {
    // create a map with all children that need to be removed
    var updates = {};
    snapshot.forEach(function(child) {
      updates[child.key] = null
    });
    // execute all updates in one go and return the result to end the function
    return ref.update(updates);
  });
});

Note:

This sample code will get executed every time an entry is changed or added to the Firebase key or any sub-key under it.

You will need to save this code under the Firebase Functions and start it.

2 Likes

Hi, sorry but I’m struggling a bit in the dark … question: but the functions of firebase are paid? if I am not misunderstood, I have to create a function in Google Cloud Platform
Is that where I have to save the function and start it?
Thanks for what you are doing.

1 Like

They are a paid feature but in most cases, until you are full blown in production, it will remain free and you only need to keep a payment method on file. I set my billing alert at 5$ and shut things down if it ever surpasses that. (Once I had a runaway function cost me 75 bucks cause it ran for 3 days straight and kept calling itself over and over with multiple concurrent invocations. It should’ve ran for 4 seconds only. And been free

2 Likes

No, you do not need to do that.

You will need to copy the script I shared in the Functions option in Firebase. See the image.
image

1 Like

… because I’m not as advanced as you are, my solution was simpler: when I save data on Firebase, my object always has as one of his property time of creation (in seconds since 1970). When I acces Firebase, i check seconds since 1970 from now compared with that property. If the result is bigger than what I want to be kept in my database, will delete that object :blush:
Firebase is very fast when working with JSON objects, so checking huge amount of data is made in a second.

2 Likes

I like this approach too @mimostel, depending on the use case!

Wanna post a block example? Great suggestion.

You too @muneer !!!

Grazie Muneer, pero nelle funzioni di Firebase non c’è la possibilità di scrivere un codice. trovo solo funzioni pre impostate.

1 Like

Of course.
This is my Firebase structure (to understand my next blocks)


At least a part of it… I keep it simple, other properties are not useful for my demo.
And this are my blocks:

Again, I removed a lot of other commands, these are only that are related to out discussion (I hope I didn’t removed anything relevant).
In my example, I clean anything older than 90 days.
P.S. Of course, this is a beginner solution… @muneer `s advice is much more… elegant :wink:

2 Likes

See this YouTube video to help you start with Firebase Function


@mimostel
Building the function in the app is a quick way but it has its own downside. Let us say you started clearing entries older than 90 days, after a while you or your users demanded more data to be available so you decided to have another month available and therefore you changed the app to delete after 120 day.

Now, you face an unpleasant issue. You need to change your code and publish an update and from now on, this feature is at the mercy of users updating the app in their devices and only until all users update the app you cannot keep that extra month of entries. Not only that if even after a year one of the users started an old copy of your, you will immediately lose the extra month of data.

Using the Firebase Function provides a clean an easy solution to this case because you only need to update the function in your database and all users will get the updated effect without even updating the app from the Store.

2 Likes

I second the sentiment of this post 100%. I love fire base functions, to be quite honest with you. If ever there was a reason to learn JavaScript it would be so that you can type Firebase functions in with your phone cable app use fire store and really super charge the heck out of everything.

Unfortunately that’s not an nocode approach. You could however learn how to get basic functions up relatively quickly, take some free online JavaScript courses and really get going.

1 Like

I totally agree. My approach is almost 1 year old (when I started the topic) and can be only apply in certain situation, as my app, when 90 days is already 10 times longer than needed anyway.
On the other hand, some data i don’t want to be deleted no matter how old they are - well, I have another property to check and Skip deletion process - if i want some relevant contest to remain forever
I don’t know if this will be possible with your approach, but I guess that yes - but that is involving more coding, here, where i come for no coding :face_with_hand_over_mouth: (started to look on that video tutorial and i already got a headache :face_with_hand_over_mouth:) + a billing plan.
P.S. as for the mess can be if no all users update their app… Solved that already. As you can see in my Firebase structure, i have something called: app versions. Every time I update an app, I put there the latest version. When a user start his app, on login screen is a label with app version. If that version fails to check with firebase written app version, he is invited to update the app. I can just to not let go further than login screen an outdated app :thinking: - just like a mobile game that can’t be played until updated if an update occurs.

2 Likes

Hello Mimostel. I have tried in every way to create what you have done. but nothing doesn’t work for me. Do you have an example of a project that you can use? so as to be able to create what you did too.
Thank you

Hello.
Post no.15 is the way I used if you are looking for an example… :thinking:

1 Like