Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Gmail: delete old emails automatically

Gmail: delete old emails automatically

Automatically deletes old emails that match the specified label.

Get started

  • Create a new Google Apps Script at
  • Overwrite the placeholder with the javascript below
  • Update the following constants:
    • LABEL_TO_DELETE: the label that should be have old messages deleted
    • DELETE_AFTER_DAYS: the age of messsages after which they will be moved to trash
  • Save the script, then run:
    • Initialize
    • Install

If you ever want to remove the script, run Uninstall to remove any left over triggers.



  • Added support for multiple labels
  • Added configurable TRIGGER_NAME
  • Increased default page size
  • Decreased default delay between receipt and delete


  • Removed use of deprecated Session.getTimeZone()
  • Improved efficiency for long threads by checking thread.getLastMessageDate()


H/T: Arun's post How to Auto Delete Old Emails In Any Gmail Label

// The name of the Gmail Label that is to be checked for purging?
var TRIGGER_NAME = "dailyDeleteGmail";
// Purge messages in the above label automatically after how many days?
// Maximum number of threads to process per run
var PAGE_SIZE = 150;
// If number of threads exceeds page size, resume job after X mins (max execution time is 6 mins)
function Intialize() {
function Install() {
// First run 2 mins after install
.at(new Date((new Date()).getTime() + 1000*60*2))
// Run daily there after
function Uninstall() {
var triggers = ScriptApp.getProjectTriggers();
for (var i=0; i<triggers.length; i++) {
function dailyDeleteGmail() {
var age = new Date();
age.setDate(age.getDate() - DELETE_AFTER_DAYS);
var purge = Utilities.formatDate(age, TIMEZONE, "yyyy-MM-dd");
var search = "(label:" + LABELS_TO_DELETE.join(" OR label:") + ") before:" + purge;
Logger.log("PURGE: " + purge);
Logger.log("SEARCH: " + search);
try {
var threads =, 0, PAGE_SIZE);
// Resume again in 10 minutes
if (threads.length == PAGE_SIZE) {
Logger.log("Scheduling follow up job...");
.at(new Date((new Date()).getTime() + 1000*60*RESUME_FREQUENCY))
// Move threads/messages which meet age criteria to trash
Logger.log("Processing " + threads.length + " threads...");
for (var i=0; i<threads.length; i++) {
var thread = threads[i];
if (thread.getLastMessageDate() < age) {
} else {
var messages = GmailApp.getMessagesForThread(threads[i]);
for (var j=0; j<messages.length; j++) {
var email = messages[j];
if (email.getDate() < age) {
} catch (e) {}
Copy link

Thanks a lot @jamesramsay

Copy link

Thanking a lot for this amazing code. I am using it to get rid of hundreds of emails on a regular basis AUTOMATICALLY! This script has saved a lot of effort and increased productivity. I wrote a post about it here:

Initially, 4 months ago, I had a problem deleting emails from multiple labels when I was putting the names of multiple labels in the script and it didn't delete any of them at all. But, Just now, at this very moment, I found a 10-year old answer on , saying that we need to add '-' sign if the label names have spaces in them. And now, it is working properly as it should. just writing it here because if anyone else faces the same problem, it will help them with that tiny issue.

Thanks again, @jamesramsay !

Copy link

Thank you, helps a lot!

Copy link

This has been awesome up until about 5 months ago when gmail changed some permission stuff. I'm trying to get working again. I want to tell script to delete Inbox after 1000 days. Is there a way to do this? Adding 'Inbox' as such
// The name of the Gmail Label that is to be checked for purging?
does not work. Any body know how to achieve this?

Copy link



That is the correct tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment