-2

Experts, I need your help. Thanks to all that weigh in.

I have run in to a strange situation. When I try to run an unbound script in my personal google account, I get a "This app is blocked" error.

I have 39 folders in my personal gmail account, each containing critical email from a different sender. The folders contained emails that were getting outdated, so I am using the script below to delete the older emails:

/*
    This script, when used with Google Apps Scripts will delete 500 emails and
    can be triggered to run every minute without user interaction enabling you
    to bulk delete email in Gmail without getting the #793 error from Gmail.
    Configure the search query in the code below to match the type of emails
    you want to delete
    Browser to https://script.google.com/.
    Start a script and paste in the code below. 
    After you paste it in, save it and click the little clock looking button.
    This is for your triggers. You can set up how frequently you want the script
    to run (I did mine for every minute).
    Source : # https://productforums.google.com/d/msg/gmail/YeQVDuPIQzA/kpZPDDj8TXkJ
    */
    
    function batchDeleteEmail() {
      var batchSize = 100 // Process up to 100 threads at once
      var threads = GmailApp.search('label:************ AND older_than:6m AND is:read');
      for (j = 0; j < threads.length; j+=batchSize) {
        GmailApp.moveThreadsToTrash(threads.slice(j, j+batchSize));
        }
    }

I needed a duplicate of this script to work on each of the 39 folders, so I installed the script, pointed the script to folder #1, ran the script to authorize it, and set up my trigger (every month on 1st).

Then I copied the script and repeated the setup process for folder #2, then folder #3, and so on... Remember, these are all UNBOUND scripts in my PERSONAL account.

Experts, why is this happening? Better yet, how do I fix it? (Is it possible to "loop" through each of the 39 folders in turn? The search terms never change, only the folder name.)

I'm lost here. Does anybody have any insight?

user3279926
  • 59
  • 2
  • 12
  • I don't understand what you imply by *folders*. Do you mean Gmail tags? – Jacques-Guzel Heron Oct 27 '21 at 07:48
  • My most humble apologies, kind sir. Thank you for calling me out. I used the wrong word. Yes, I meant to say "I have 39 **labels** in my Gmail account, each containing..." Now that I am a little bit more awake and a little less angry but still frustrated, I see other places that I should have said things differently... – user3279926 Oct 27 '21 at 14:46
  • 1
    It should be possible to loop through. But, this error needs to be reported to Google. See https://stackoverflow.com/questions/66633659/can-not-run-trivial-app-script-bound-to-new-blank-document-get-this-app-is-bl/69701477#69701477 – TheMaster Oct 27 '21 at 20:34
  • @user3279926 did the answer shared by TheMaster helped you? Please test it and share your findings here. – Jacques-Guzel Heron Nov 08 '21 at 13:32

1 Answers1

0

You can easily modify your function to loop over every label just by hardcoding them into an array. You can use the following example as is, you would only need to modify the labels.

function batchDeleteEmail() {
  var batchSize = 100 // Process up to 100 threads at once
  var labels = ["MyLabel1", "MyLabel2", "MyLabel3", "MyLabel4", "MyLabel5",
    "MyLabel6", "MyLabel7", "MyLabel8", "MyLabel9", "MyLabel10", "MyLabel11",
    "MyLabel12", "MyLabel13", "MyLabel14", "MyLabel15", "MyLabel16",
    "MyLabel17", "MyLabel18", "MyLabel19", "MyLabel20", "MyLabel21",
    "MyLabel22", "MyLabel23", "MyLabel24", "MyLabel25", "MyLabel26",
    "MyLabel27", "MyLabel28", "MyLabel29", "MyLabel30", "MyLabel31",
    "MyLabel32", "MyLabel33", "MyLabel34", "MyLabel35", "MyLabel36",
    "MyLabel37", "MyLabel38", "MyLabel39"
  ];
  
  for (var i = 0; i < labels.length; i++) {
    var query = "label:".concat(labels[i].concat(
      " AND older_than:6m AND is:read"));
    var threads = GmailApp.search(query);

    for (j = 0; j < threads.length; j += batchSize) {
      GmailApp.moveThreadsToTrash(threads.slice(j, j + batchSize));
    }
  }
}
Jacques-Guzel Heron
  • 2,480
  • 1
  • 7
  • 16
  • Jacques-Guzel Heron, Your code can be saved without a problem, but when I run the project (with the actual label names, of course), I get the following error: **This app is blocked This app tried to access sensitive info in your Google Account. To keep your account safe, Google blocked this access.** How can I get past this? – user3279926 Dec 19 '21 at 17:39
  • I had a feeling that the authorization problem (and its resulting error) might have been caused by copying and pasting. (That is how I "placed" your script into the GAS editor.) I deleted the copy-and-paste version of your script, and reinstalled your script again, this time manually typing each line. There was no change. I got the same error, which told me that my 'feeling' was wrong. And so, now I'm back at square one. How do I get past this? – user3279926 Dec 19 '21 at 19:38