Kanta's Blog🇩🇪🇯🇵
LinkedIn

Email Cleanup Apps Script Job (Gmail + Apps Script)

Email is one of the primary stressors, according to many studies. However, you often need to check your inbox, whether personal or professional. While some mailing lists offer easy unsubscribe options, others do not. Additionally, some lists mix important updates with unimportant ones, using the same email alias.

Leveraging the power of Google Apps Script and Gmail, you can create a script that automatically identifies unimportant emails based on your defined rules. These emails can then be marked as read and archived, saving you the time and stress of sifting through junk. By streamlining your inbox, you can focus on more important tasks and enjoy a more productive day.

To implement this solution, simply paste the script into a standalone Apps Script project and set up a time-based trigger :)

function cleanUpEmailBox() {
  const queryLibrary = ["to:me is:unread from:jobalerts-noreply@linkedin.com newer_than:2d",
    'to:me is:unread from:no-reply@researchgatemail.net subject:"recommended this preprint" newer_than:2d']

  queryLibrary.forEach(searchQuery => {
    console.log("Using the following query:" + searchQuery)
    const threads = GmailApp.search(searchQuery)
    const rateLimit = 50
    let subjectTitles = []
    threads.forEach((thread, threadIdx) => {
      if (threadIdx >= rateLimit) return // Break the loop if it exceeds to rate limit
      subjectTitles.push(thread.getFirstMessageSubject())
      thread.markRead()
      thread.moveToArchive()
    })
    if(subjectTitles.length > 0){
          console.log("Action summary: marked read and archived the following emails:")
    console.log(subjectTitles.join("\n"))
    }else{
      console.log("No emails found via rule-set. No actions done.")
    }
  })

}