How do I clean up old data with a cron job?
Schedule a daily task at midnight (cron.schedule('0 0 * * *', ...)). Use MongoDB queries to delete documents older than a threshold: await User.deleteMany({ createdAt: { $lt: thirtyDaysAgo } }). Clean up expired tokens, unverified users, and old connection requests.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Common Cron Job Use Cases in Node.js Cleanup, Notifications, and Reports
Data cleanup (delete old tokens, unverified users, expired records), email notifications (daily digests, renewal reminders), report generation (weekly analytics), database backups (daily mongodump to S3), cache warming, log rotation, and health checks (verify database and Redis connectivity).
Schedule at 9 AM (cron.schedule('0 9 * * *', ...)). Find users with pending connection requests from the last 24 hours. For each user, send a digest email with their pending requests. Check emailBounced and emailComplained flags before sending.
Schedule at 2 AM (cron.schedule('0 2 * * *', ...)). Use child_process.exec to run mongodump with --uri and --archive --gzip flags. Upload the backup file to S3. Keep local backups for 7 days and S3 backups for 30 days.
Still have questions?
Browse all our FAQs or reach out to our support team
