2

This talk mentions time-expiring data using Firebase rules at 22:55

https://www.youtube.com/watch?v=PUBnlbjZFAI

How can one do this ?

I didn't find any information regarding this.

  • 1
    Probably because external links are generally frowned upon as they expire which then invalidates the question. It would be better to ask a more thorough question, present the code you have attempted and a Firebase structure you are working with (as text, no images). If you have not read it, I would suggest reading [How to ask a good question](http://stackoverflow.com/help/how-to-ask) – Jay Jan 10 '17 at 20:20
  • @Jay Ahhhhh... Sometimes I feel this is a bit too strict. In this case, there is literally no MVE to be shown since I found zero data on what I am looking for. All I have is this one mention of about 2 seconds in this video where a Firebase Engineer says "this is possible", without saying how and then moves on to another subject. –  Jan 10 '17 at 21:15
  • It would be good to understand what your use case would be with time-expiring data as it could mean a 10 different things. – Jay Jan 10 '17 at 22:01
  • @Jay My posts should disappear after a week if the score is lower than 100. –  Jan 10 '17 at 22:04
  • 1
    That's not possible with Firebase as there is no server side logic to perform that function for the database. See [Delete Firebase Data after set time, regardless of app termination](http://stackoverflow.com/questions/37083649/delete-firebase-data-after-set-time-regardless-of-app-termination). There are other solutions but they are driven by client side code, not server side. – Jay Jan 10 '17 at 22:17
  • @Jay But why is it mentioned by a Firebase Engineer then ? –  Jan 10 '17 at 22:21
  • What's the time-code in the video for when it is mentioned and we'll take a look. – Jay Jan 10 '17 at 22:33
  • @Jay The time is: 22:55 –  Jan 10 '17 at 23:52
  • I saw that. I don't believe the meaning is data that auto-deletes itself. It means data that cannot be read after a certain time. Firebase doesn't have server side code so it's not going to be possible to auto-delete data from the server side - it will need to handled from the client side. – Jay Jan 12 '17 at 18:28
  • @Jay I am using node.js. There must be a solution using node.js –  Jan 12 '17 at 19:46
  • See also https://stackoverflow.com/questions/37501870/how-to-delete-firebase-data-after-n-days – Kato Feb 01 '19 at 16:19

3 Answers3

3

I recommend two solutions.

1) Use cloud functions to record a message path and the date it was posted. Then every hour sort that list by date, pick all the expired ones, and create a deep update object to null out every expired message. Nowadays you can use Cron Scheduler to handle the periodic flush.

2) Make a rule that says anyone can delete expired messages and make it so that clients automatically delete expired messages when they are in a chat room.

Luke Pighetti
  • 4,541
  • 7
  • 32
  • 57
2

Written here: https://firebase.google.com/docs/database/security/securing-data

You can't have it auto delete your data but you can make them unreadable (which is the same thing from the user standpoint). Just send a timestamp child field with you data and check against it.

 {
  "rules": {
    "messages": {
      "$message": {
        // only messages from the last ten minutes can be read
        ".read": "data.child('timestamp').val() > (now - 600000)",

        // new messages must have a string content and a number timestamp
        ".validate": "newData.hasChildren(['content', 'timestamp']) && newData.child('content').isString() && newData.child('timestamp').isNumber()"
      }
    }
  }
}
  • Not what I am looking for (only looking for data deletion to save space costs and optimize search times) but thanks for the answer :D I know there are actually way to do what I am looking for, I just don't remember how. –  Jan 11 '17 at 19:05
  • 1
    Note that this only allow reads for individual message items and will invalidate reads on the "messages" collection as a whole. So this is actually ... useless – slimbikr Nov 29 '18 at 01:54
  • as slimbikr says this approach makes it impossible to read all messages if a single one has expired. it does not filter out only expired messages. – Luke Pighetti Jun 23 '19 at 17:04
0

Same question here.

You can't do it using firebase rules. You should either have a NodeJS backend removing your old data or clients doing it for you. For example, before a client retrieves data, he could remove old data.

Community
  • 1
  • 1
Spri
  • 97
  • 1
  • 2