Image by Julio César Velásquez Mejía from Pixabay

Cleanup Your Gmail With Google Apps Script

Laura Taylor
3 min readSep 2, 2021

--

If you’re like me, you have numerous unprocessed emails in your inbox. Sometimes it’s overwhelming. What to do? Spend hours processing each email in the Gmail interface or use a simple tool like Google Apps Script to speed the process. Google Apps Script is so versatile we often forget it can perform some of the most mundane tasks like assisting with email cleanup. So simple yet so powerful.

How to Setup Email Cleanup

Step 1: Open a Google Sheet

Step 2: Open the script editor (under the Tools menu) and copy the following code replacing the code in Code.gs.

The code has four options: Archive Email, Delete Email, Add a Label, Remove a Label.

/*** This function creates a menu in the toolbar*/function onOpen() {const ui = SpreadsheetApp.getUi().createMenu(‘Gmail Menu’).addItem(‘Archive Email’, ‘archiveEmail’).addItem(‘Delete Email’, ‘deleteEmail’).addItem(‘Add Email Label’, ‘addLabel’).addItem(‘Remove Email Label’, ‘removeLabel’).addToUi();}/*** This function archives email*/function archiveEmail() {const ss = SpreadsheetApp.getActiveSpreadsheet();const sheet = ss.getSheets()[0];const searchTerm = sheet.getRange(“A1”).getValue();GmailApp.search(searchTerm).map(thread => thread.moveToArchive());}/*** This function deletes email*/function deleteEmail() {const ss = SpreadsheetApp.getActiveSpreadsheet();const sheet = ss.getSheets()[0];const searchTerm = sheet.getRange(“A1”).getValue();GmailApp.search(searchTerm).map(thread => thread.moveToTrash());}/*** This function adds a label to email*/function addLabel() {const ss = SpreadsheetApp.getActiveSpreadsheet();const sheet = ss.getSheets()[0];const searchTerm = sheet.getRange(“A1”).getValue();const l = sheet.getRange(“B1”).getValue();const label = GmailApp.getUserLabelByName(l);GmailApp.search(searchTerm).map(thread => thread.addLabel(label));}/*** This function removes a label from email*/function removeLabel() {const ss = SpreadsheetApp.getActiveSpreadsheet();const sheet = ss.getSheets()[0];const searchTerm = sheet.getRange(“A1”).getValue();const l = sheet.getRange(“B1”).getValue();const label = GmailApp.getUserLabelByName(l);GmailApp.search(searchTerm).map(thread => thread.removeLabel(label));}

Step 3: Reopen the Google Sheet and wait for the “Gmail Menu” to appear.

Step 4: Enter a Gmail search term in cell A1 of the first sheet of the Google Sheet. If you are adding or removing labels, add the label in cell B1.

Step 5: Run one of the “Gmail Menu” options and follow the authorization prompts.

Step 6: Run the appropriate email cleanup option from the menu and wait for it to complete. NOTE: If you have a number of emails to process the script may timeout in which case you’ll have to wait for your quota to renew.

If you wish to run the script on a schedule, setup a timed trigger from the Google Apps Script console.

Happy email cleanup!

Looking for the code? See the TSCodeTalk_Gmail code on Github.

Need help automating workflow inside your G Suite domain? Get in touch

Follow me on Twitter, Medium and Github.

Enjoy this post? Please consider buying me a coffee. Thank you!

--

--

Laura Taylor

Google Developer Expert for G Suite passionate about Workflow Automation and Productivity — https://www.tech-streams.com