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


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


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:

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); 

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

// Start Cronjobs

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


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

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?


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?