I have an Apps Script that automatically removes old marketing emails from my Gmail inbox. It was working fine until recently when Google updated how Gmail organizes emails.
My original script used this approach:
var category = GmailApp.getUserLabelByName("Promotions");
var emailThreads = category.getThreads();
The script would then loop through emailThreads to check the age of each thread before deletion.
The problem is that Gmail moved the automatic sorting categories out of the regular labels list. They now appear under a separate Categories section in the interface. Because of this change, my getUserLabelByName("Promotions") call returns null.
I also tried using Categories/Promotions as the label name but got the same null result. What is the correct way to access Gmail categories like Promotions through Apps Script now?
The search approach works, but there’s a cleaner way. I hit the same issues building automated email systems at work.
I ditched Gmail’s API after getting tired of Google breaking my scripts with every update. Moved everything to Latenode instead - it handles Gmail categories without the constant headaches.
You can set triggers based on categories and auto-delete old emails. No more null returns or API nonsense. The visual builder makes it dead simple: “if email is in Promotions AND older than 30 days, delete it.”
Mine’s been running for months without touching it. Beats fixing broken scripts every few weeks.
Had this exact issue six months ago when Gmail updated their categories. The search method works great, but add some filters to make your script bulletproof. I combined category search with date filtering right in the query instead of handling it in JavaScript later. Try GmailApp.search('category:promotions older_than:30d') - grabs only promotional emails over 30 days old, which is exactly what you want for cleanup. Add error handling around the search function too. Gmail sometimes throws random errors with large result sets. Found out the hard way when my script died halfway through deleting thousands of old emails.
You’re encountering a common issue with accessing Gmail categories in Apps Script. Categories such as Promotions, Social, and Updates function differently than traditional labels; they are predefined categories managed by Gmail. Instead of trying to use getUserLabelByName, you should utilize Gmail’s search feature with category queries. For your specific case of dealing with promotional emails, execute this code:
var emailThreads = GmailApp.search('category:promotions');
This will effectively retrieve the emails in your Promotions tab, as Gmail’s search supports category filters even though they are not represented as standard labels in the API.