Skip to content

Instantly share code, notes, and snippets.

@chxru
Created October 7, 2023 07:18
Show Gist options
  • Save chxru/3525322af3f0ff821d6f91fb0c3ce86a to your computer and use it in GitHub Desktop.
Save chxru/3525322af3f0ff821d6f91fb0c3ce86a to your computer and use it in GitHub Desktop.
Auto delete old IESL emails from gmail
function auto_trash_old_iesl_emails() {
const emails = GmailApp.search('from:ieslweb@iesl.lk')
if (emails.length === 0) {
// return if no email found
console.log("No emails found!")
return
}
for (const email of emails) {
const lastUpdateOn = email.getLastMessageDate()
const age = ((new Date()) - lastUpdateOn) / (1000 * 60 * 60 * 24) // in days
if (age > 7) {
console.log('Moving ', email.getFirstMessageSubject(), ' to trash, ', age, ' days old')
email.moveToTrash()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment