Skip to content

Instantly share code, notes, and snippets.

@ayhc
Last active April 30, 2021 14:15
Show Gist options
  • Save ayhc/e466d690968fd40f0f3eda3373b9e6a2 to your computer and use it in GitHub Desktop.
Save ayhc/e466d690968fd40f0f3eda3373b9e6a2 to your computer and use it in GitHub Desktop.
A simple, kludgey bellend blocker Google Apps Script for Gmail (trash or completely delete emails by label).
//A simple, kludgey bellend blocker, for Google Apps Script. Use in conjunction with Gmail email filters.
//Loosely based on work by boly38: https://stackoverflow.com/questions/36377391/script-to-permenantly-delete-my-emails-with-google-script
//No rights reserved.
function bellendBlocker() {
Logger.log ('Starting Bellend Blocker...')
//Basic settings: which email address, what label, what to do with matching emails
var mymail = [PUT YOUR EMAIL ADDRESS HERE];
var mylabel = [PUT YOUR EMAIL LABEL HERE];
var purgeNotDelete = [TRUE OR FALSE];
//Generate an array of matching emails and associated variables
var messagesList = Gmail.Users.Messages.list(mymail, {
q: 'label:' + mylabel,
});
//See what the messages list looks like
//Logger.log(messagesList);
//If there are matching emails...
if (messagesList.resultSizeEstimate != 0) {
//Generate variables
var totalMessages = messagesList.messages.length;
Logger.log('Total messages: %s', totalMessages);
var currentEmail;
//The actual message-deleting bit
Logger.log ('Permanently deleting: %s', purgeNotDelete)
for (messagesDeleted = 0; messagesDeleted < totalMessages; messagesDeleted++) {
var currentEmail = messagesList.messages[messagesDeleted].id;
Logger.log ('Processing: Email ID %s', currentEmail)
if (purgeNotDelete) {
Gmail.Users.Messages.remove(mymail, currentEmail);
Logger.log('Purged: Email ID %s', currentEmail);
} else {
Gmail.Users.Messages.trash(mymail, currentEmail);
Logger.log('Moved to trash: Email ID %s', currentEmail)
}
Logger.log ('Processed: %s messages', (messagesDeleted + 1));
}
Logger.log ('All done!');
return;
} else {
//If there are no matching emails
Logger.log ('Nothing yet; done for now.');
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment