en-UShe-IL
You are here:   Blog
Register   |  Login

Blog Archive:

Maximize
* Can be used in order to search for older blogs Entries

Search in blogs


Blog Categories:

Maximize
* Can be used in order to search for blogs Entries by Categories

Blog Tags:

Maximize
* Can be used in order to search for blogs by keywords

TNWikiSummit


Awared MVP 


 


Microsoft® Community Contributor 


Microsoft® Community Contributor


 Read first, before you you use the blog! Maximize
אוג16

Written by: ronen ariely
16/08/2019 07:20 RssIcon

I wrote this short script for my personal use. I had over 60k unread massages in specific label and I wanted to mark all the messages before 01/01/2019 as read.

Yes you noticed correct... I have more in other labels! sry but I cannot read all the emails which I get and they just keep coming. If you need me then please contact me on Facebook, linkedin, or Skype.

The problem is that using the GUI Gmail allows us to see maximum 100 emails in a page and manually delete 100 emails at a time might need several hours to end the task. 

Therefore, I choose to use Google script for the task...

But here I ran into another limitation: Google limit the maximum of email returns in the search command "GmailApp.search" to 500. The solution I chose was to use a simple loop and mark 100 emails in each iteration.

function Intialize() {
  return;
}
 
function Install() {
  var Text2Write = new Date();
  ArielyWriteLog(Text2Write);
 
  var max = 99;
  var offset = 0;
   
  while (true) {
    var threads = GmailApp.search("label:MyLabelFilterHere is:unread before:01/01/2017 after:01/01/2016", offset, max);
    GmailApp.markThreadsRead(threads);
    if (threads.length < max) {
      break;
    }
    ArielyWriteLog("max" + max);
    Utilities.sleep(100);
    offset += max;
  }
}
 
 
function ArielyWriteLog(Text2Write) {
  var MyFileName = 'Ronen Ariely Log Spam Emails';
  var files = DriveApp.getFilesByName(MyFileName);
  var file = !files.hasNext() ? DocumentApp.create(MyFileName) : files.next();
  var doc = DocumentApp.openById(file.getId())
  doc.getBody().appendParagraph(Text2Write);
}