Roger99
Roger99

Reputation: 992

How to delete data from a Firebase database on a schedule

I have created a simple chatroom-like Apache Cordova (HTML) application that allows users to add their own messages with custom usernames in the chatroom.

The messages add up after a while and i have to go into the actual database to delete them every day to avoid clutter.

Is there any way i can create a rule or a javascript function to clear the data at a certain time every day? For instance at 9 am the database would clear all its content from the previous day and night and start the day fresh.

Thanks for the help.

Upvotes: 0

Views: 2477

Answers (1)

Jonathan Lin
Jonathan Lin

Reputation: 124

From the looks of it there are two current similar solutions: Creating Scheduled Task in JavaScript || Later.js execute Function on Schedule?

I am unfamiliar with later.js so I can't help you there.

Using gdoron's code, I believe the code should look similar to this:

function foo(){
    var day =new Date().getDay();
    var hours =new Date().getHours();

    if (day === 0 && hours >12 && hours < 13)  // day is a 0 index base
                                               // sunday between 12:00 and 13:00

//What you want to do goes here
var fredRef = new Firebase('yourDirectoryUrl')
fredRef.remove();

}

setInterval(foo, 3600000); // one hour check.

Notes:

  • Remember to include

    <script src='https://cdn.firebase.com/js/client/2.2.1/firebase.js'></script>
    

  • Also, keep in mind this will clear EVERYTHING.
  • Upvotes: 2

    Related Questions