Search code examples
google-apps-scriptgmail

Script for autoarchiving gmail deleted all of a sudden


I have a simple script that autoarchives emails with certain labels created by filters. A few days ago I noticed my inbox began to look crowded and noticed the script wasn't running... When I went to script.google.com the project was basically gone, it's not even in the trash.

Does google delete scripts like that without warning? I have added it again and it's working but it was surprising to me how it disappeared all of a sudden.

Any ideas?

Here's the script:

function gmailAutoarchive() {
  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?

  archiveLabel("Bank",maxDate);
  archiveLabel("Receipts",maxDate);
  archiveLabel("Misc",maxDate);
}

function archiveLabel(label, maxDate) {
  var label = GmailApp.getUserLabelByName(label);
  if (label == null) {
    return;
  }
  var threads = label.getThreads(0, 400);

  // 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++) {
    if (threads[i].getLastMessageDate() < maxDate) {
      threads[i].moveToArchive();
    }
  }
}

Solution

  • From the question

    Does google delete scripts like that without warning?

    No.

    First, search for your script in the Google Drive trash. Second, see if there apps connected to your account that have access your files in Google Drive. If there are, then maybe one of this apps deleted your file.

    In case that the script is found in the trash, restore it, if not contact Google Drive support (look at https://support.google.com/drive for instructions)