Automatically Archive Gmail Labels with Google Scripts zhijie, September 30, 2023September 30, 2023 I enjoy having a clean inbox and I am currently using Google Scripts to clean up my inbox. Here is the URL to Google’s App Script: My Projects – Apps Script (google.com) Documentations This is the main documentation used to create this script: Documentation to Gmail Service: Gmail Service | Apps Script | Google for Developers Getting Started There are a few steps to start running the script to auto archive your emails. Create a project in Copy the google script to https://script.google.com Create a Time-based trigger Creating a new project Go to https://script.google.com and create a new project. Name your project. In my case, I am calling it GmailAutoArchive. Copying the google script Here is the code that I am currently using to archive my labelled mails. Copy and replace the default functions. Then press Ctrl + S to save. /* * Author: Zhi Jie * Last Update: 30 Sept 2023 * References: https://developers.google.com/apps-script/reference/gmail */ function gmailAutoarchive() { var threadSize = 200; // Getting Array of user created labels var labels = GmailApp.getUserLabels(); // Processing all labels for (var a = 0; a < labels.length; a++) { Logger.log("Processing " + labels[a].getName()); // Get List of var threads that are unread && inInbox var threads = labels[a].getThreads(0, threadSize).filter( function (thread) { return (!thread.isUnread() && thread.isInInbox()); }); GmailApp.moveThreadsToArchive(threads); Logger.log("Completed " + labels[a].getName()); } } Setting up Authorizations The first time you try to run the script, you will run into this error. Click review permissions, then sign in to your account again. Then, it will show you this prompt. Click on advanced, then go to project (unsafe). Next, allow the project’s permission to run. Executing for the first time You should see an execution log pop up from below. Then it should process your email inbox archiving all the labeled items in your inbox. Setup Triggers Lastly, we would want this script to execute automatically to allow the labels to be automatically archived. Click on the Triggers on the left. Click Add Trigger at the bottom right of the screen. Setup the trigger as seen below or to your liking. For me I would like the script to automatically archive the labels every day at 2-3am. Conclusion The script will be executing as you have setup above. Feel free to modify the scripts to suit your needs. Share this:FacebookXLike this:Like Loading... Related Blog gmailgoogle scripts