1

Update: Umm, something seems to have happened. Now my Inbox has gone from 36,000+ to 353,000+ messages in it. Huh? What on earth is going on here.


I've never archived my Gmail Inbox and as a result have 36,000+ messages in it. I'd like to archive all messages over 60 days old, and no way I'm doing it manually.

So after some googling I found a google-script that could do it, below. However, GmailApp.moveThreadsToArchive(threads); can apparently can only be called on up to 100 threads at a time, so I've tried wrangle it into a while loop that calls it many times on 100 threads a time:

/**
* Archives Emails older than a given time interval
* src:  http://www.quora.com/Gmail/Is-there-a-way-to-auto-archive-emails-after-a-certain-number-of-days
* 
* example:
*   RunAutoArchive('2w')
*   RunAutoArchive('1m')
*   RunAutoArchive('5y')
*/

function RunAutoArchive(){

  // archive messages older than:
  var interval='60d';

  // number of threads
  var threadCount = 0;
  var start = 0; 
  var end = 100;

  // find messages older than a certain time
  if(interval != ""){
    var searchQuery = 'in:Inbox older_than:'+interval;
    var threads = GmailApp.search(searchQuery);    
    threadCount = threads.length;

    // if there are any threads
    if(threadCount > 0){
      while(start <= threadCount){

        // move threads to archive
        GmailApp.moveThreadsToArchive(threads.slice(start,end));

        // increment count
        start += 100;
        end += 100;

      }
    }
  }

  // refresh threads
  GmailApp.refreshThreads(threads);

  return threadCount;
}

However, it both debugs and runs successfully (very long Execution Transcript ends with [14-01-16 21:40:59:199 PST] Execution succeeded [284.472 seconds total runtime]), but my inbox remains unchanged - 36,000+ unarchived messages remain.

Any suggestions what might be wrong?

bgibson
  • 17,379
  • 8
  • 29
  • 45
  • 1
    There are actually limits on how many emails can be handled at once. This question may be able to help you out a little (even though it tried to solve a completely different issue that was a former bug): http://stackoverflow.com/questions/15234882/need-help-optimizing-a-google-apps-script-that-labels-emails – Claudia Jan 18 '14 at 05:46
  • You would have to adapt it a little, though, to your needs. – Claudia Jan 18 '14 at 05:48
  • I would also like to point out that your script doesn't take into account the possibility of you having a (likely) incomplete batch as the last one. Also, you have some useless clutter in your script: your interval variable will never be an empty string because you set it as such, and you don't need to declare your thread count to 0 initially, because if your search brings nothing, you'll get an array of zero length. The latter isn't needed when you remove the unnecessary if-statement. – Claudia Jan 18 '14 at 05:57

1 Answers1

0

To move inbox gmail threads into archive after x number of days, you could perhaps use the following and set a trigger every minute to archive large number of incoming emails, just set the scriptproperties key "CNT" to 0 and run the following scripts

function gmailAutoarchive1() {

  var delayDays = 2; // will only impact emails more than 48h old
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate()-delayDays); // what was the date at that time?
  Logger.log("MAXDATE",maxDate);

  // Get all the threads labelled 'autoarchive'
  var label = GmailApp.getUserLabelByName("AUTOARCHIVE");

  var scriptProperties = PropertiesService.getScriptProperties();
  var cnt=scriptProperties.getProperty("CNT");
  var cnt_val=parseInt(cnt)


//  var threads = label.getThreads(cnt_val,499);
   var threads = GmailApp.getInboxThreads(cnt_val,499);

  // we archive all the threads if they're unread AND older than the limit we set in delayDays
  for (var i = 0; i < threads.length; i++) {

      Logger.log("del",threads[i].getLastMessageDate());     

    if (threads[i].getLastMessageDate()<maxDate)
    {  
      threads[i].moveToArchive();
     // threads[i].moveToTrash();
    }
  }

  scriptProperties.setProperty("CNT", cnt_val+499)

}