Search code examples
google-apps-scriptgmail

Get Gmail categories


I used to have a working apps script that deleted old promotional emails in my Gmail account.

It used the code

var label = GmailApp.getUserLabelByName("Promotions");

to get the label and then it iterated through label.getThreads() to decide whether each thread was old enough to delete.

However, Google have now changed Gmail so the auto categorisations are now under the Categories section in the UI, rather than within the list of labels so the above now returns null.

How can I fix my code to retrieve the Promotions category? I have tried Categories/Promotions too but it also comes up null.


Solution

  • Gmail categories can easily be searched.

    Here is a small code that looks for every promotion mail. The result is an array of threads, you can add Label to each of them so that your old script will be happy again ;-)

      var threads = GmailApp.search('category:promotions');// check the category Gmail added to the thread
    

    documentation here