[solved] Remove old documents periodically - good way of doing it?


#1

Situation

I have one collection (Entries).
Users can create entries (Entries.insert).
Said documents get a createdAt: new Date() as a timestamp.

Goal

I want every document that is older that 3 months to be removed from collection automatically.
How do you best do such a thing?

Is that what Meteor.startup() could be used for?

Thanks for your help.


#2

I think, startup only run once the server start, you should be looking at crons instead. Either use some packages like: https://github.com/percolatestudio/meteor-synced-cron/ or put Metoer.setInterval if it is not critical.


#3

Hey @kenken,

Exactely what I was looking for :smile:
Plus: Now I also know what those mysterious cronjob-things actually do.
Many thanks!

For the sake of completion I’m posting my solution here:

SyncedCron.add({
name: 'Remove old Entries (90 Days)',
schedule: function(parser) {
	return parser.text('every 8 hours');
},
job: function() {
	var today = new Date();
	var targetDate = new Date();

	targetDate.setDate(today.getDate() - 90); 
	targetDate.setHours(0);
	targetDate.setMinutes(0);
	targetDate.setSeconds(0);

	// Remove matchng Documents
	Entries.remove({createdAt: {$lt: targetDate}});
}
});


// Start Cronjobs
SyncedCron.start();

If anyone finds an error in my approach please let me know.
Thanks.


#4

I think a better solution is to use a MongoDB TTL index. MongoDB will automatically delete documents older than given time. For your case the mongo command will look like this.

db.entries.ensureIndex({createdAt: 1}, {expireAfterSeconds: 60*60*24*90});

For more info checkout MongoDB Docs on TTL indexes. You can also create the index at meteor app startup. (I didn’t test these codes if they didn’t work but it’ll be something very similar).

// on server
Meteor.startup(function () {  
  Entries._ensureIndex({createdAt: 1}, {expireAfterSeconds: 60*60*24*90});
});

Remove items from collection if older than 10 minutes don't work?
#5

oh, thanks @mnmtanish, that’s something I didn’t know about.


#6

Pretty cool solution there.
I gave it a try and unfortunately it didn’t work.
Can anyone get this running as expected?