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

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.

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.

1 Like

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.

2 Likes

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});
});
6 Likes

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

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

2 Likes

my collection name is RecOutput_834 I want to remove one week old data every week to automate this can you help me

Thanks for the help…

You can use percolate:synced-cron to setup job to run with any type of frequency

I’m using steve jobs from @msavin It’s very useful.

i recommend to go with an index like mentioned above: [solved] Remove old documents periodically - good way of doing it?