Reputation: 97
Frank van Puffelen has an example for deleting database after time. https://stackoverflow.com/a/32012520/12253594 https://github.com/firebase/functions-samples/tree/master/delete-old-child-nodes
Which works wonders. But I'm wondering if it is possible for Storage as well?
The structure: Experiences -> Experience_ID -> fullHD -> Image
And I want to delete including Experience_ID.
I'm thinking, something like:
exports.deleteOldItems = functions.storange.ref('/Experiences/{notification_id}')
.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);
});
});
But I obviously don't have timestamps like I do in the database section. So what do I do?
Best regards,
Upvotes: 1
Views: 963
Reputation: 317760
You have two main options.
Encode the date into the name of the file or its metadata, use the Cloud Storage list api to list all possible files, and check the file name or metadata to determine if it should be deleted. This solution doesn't scale very well as the number of possible files to be listed grows very large.
Mirror some data about each file in a database. For each file that you add to Cloud Storage, make an equivalent record in a database that includes both the timestamp and the path of the file. Since databases are easy to query, you can simply query the database to find the files that should be deleted.
Upvotes: 3