I’m stuck trying to clear out my Gmail promotions tab. I found some code online and tweaked it a bit, but it’s not working. When I run it, I get this error:
Response Code: 404. Message: Not Found. (line 4, file "Code")Dismiss
Here’s the script I’m using:
function clearPromotions(user) {
var emails = GmailApp.search("label:promotions");
emails.forEach(function(email) {
GmailApp.moveThreadToTrash(email);
});
}
Can anyone help me figure out what’s wrong? I’m not great with coding, so I’m not sure if I messed something up when I changed the original script. Any tips or fixes would be really helpful!
I’ve dealt with this issue before, and it can be frustrating. One thing to consider is that the GmailApp service has limitations on how many emails it can process at once. You might want to modify your script to handle emails in batches. Here’s a snippet that worked for me:
function clearPromotions() {
var batch = GmailApp.search('category:promotions', 0, 100);
for (var i = 0; i < batch.length; i++) {
batch[i].moveToTrash();
}
}
This processes 100 emails at a time, which can help avoid timeouts. Also, make sure you’re running this as a time-based trigger rather than manually. It might take several runs to clear out everything if you have a lot of promotional emails. Remember to test on a small batch first to ensure it’s working as expected before running it on your entire inbox.
hey there! i had similar issues before. try changing ‘label:promotions’ to ‘category:promotions’ in ur search. also, make sure u’ve given the script permission to access ur gmail. if that doesn’t work, u could try using filters in gmail settings to auto-delete promo emails. hope this helps!
The error you are encountering suggests there may be issues with permissions or API access rather than the code logic itself. It would be worthwhile to confirm that the Gmail API is enabled in your Google Cloud Console and that the script has been granted proper authorization to manage your Gmail account. In my own experience, switching from using GmailApp.search to GmailApp.getInboxThreads has sometimes resolved similar issues. You might also explore Gmail’s built-in filters as an alternative approach to automatically manage label-specific emails.