Set up Firebase Cloud Functions: If you haven't already, you'll need to set up Firebase Cloud Functions in your Firebase project. You can refer to the Firebase documentation for guidance on getting started.
Create a scheduled function: In your Cloud Functions code, you can create a scheduled function that triggers at midnight using a cron-like syntax. You can use a package like node-cron or firebase-functions to achieve this.
// Schedule a function to run at midnight in the Central Time Zone
exports.deleteDatabaseAtMidnight = functions.pubsub
.schedule('0 0 *') // Trigger at 00:00 UTC (midnight)
.timeZone('America/Chicago') // Set the Central Time Zone
.onRun(async (context) => {
try {
// Get a reference to the root of your Realtime Database
const rootRef = admin.database().ref();
// Delete the entire database
await rootRef.remove();
console.log('Database deleted successfully.');
} catch (error) {
console.error('Error deleting the database:', error);
}
});
Deploy the Cloud Function: Deploy the Cloud Function to your Firebase project using the Firebase CLI. Run the following command in your project's root directory:
Steps
Set up Firebase Cloud Functions: If you haven't already, you'll need to set up Firebase Cloud Functions in your Firebase project. You can refer to the Firebase documentation for guidance on getting started.
Create a scheduled function: In your Cloud Functions code, you can create a scheduled function that triggers at midnight using a cron-like syntax. You can use a package like node-cron or firebase-functions to achieve this.
const functions = require('firebase-functions'); const admin = require('firebase-admin'); const cron = require('node-cron');
admin.initializeApp();
// Schedule a function to run at midnight in the Central Time Zone exports.deleteDatabaseAtMidnight = functions.pubsub .schedule('0 0 *') // Trigger at 00:00 UTC (midnight) .timeZone('America/Chicago') // Set the Central Time Zone .onRun(async (context) => { try { // Get a reference to the root of your Realtime Database const rootRef = admin.database().ref();
});
firebase deploy --only functions