Skip to content

Instantly share code, notes, and snippets.

@jordanlambrecht
Last active October 20, 2023 17:36
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Auto Delete / Archive Emails in Gmail
function autoDelete() {
console.log('Started autoDelete run.');
var delayDays = 2;
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("delete me");
var threads = label.getThreads();
if(threads.length > 0){
console.log('Found ' + threads.length + ' emails marked for deletion.');
var counter = 0;
try {
for(var i =0; i < threads.length; i++){
if (threads[i].getLastMessageDate()<maxDate){
Logger.log('deleted email: ' + threads[i].getFirstMessageSubject());
threads[i].markUnimportant();
threads[i].markRead();
threads[i].moveToTrash();
counter++;
}
}
console.log('Successfully moved ' + counter + 'emails to the trash.');
}
catch(e){
console.error('Could Not Start Run: ' + e);
}
}
else{
console.log('Found ' + threads.length + 'emails marked for deletion. Exiting.');
}
}
function autoArchive() {
console.log('Started autoArchive run.');
var delayDays = 2;
var maxDate = new Date();
maxDate.setDate(maxDate.getDate()-delayDays);
var label = GmailApp.getUserLabelByName("archive me");
var threads = label.getThreads();
if(threads.length > 0){
console.log('Found ' + threads.length + ' emails marked for archival.');
var counter = 0;
try {
for(var i =0; i < threads.length; i++){
if (threads[i].getLastMessageDate()<maxDate){
Logger.log('archived email: ' + threads[i].getFirstMessageSubject());
threads[i].markRead();
threads[i].moveToArchive();
threads[i].markUnimportant();
counter++;
}
}
console.log('Successfully archived ' + counter + 'emails.');
}
catch(e){
console.error('Could Not Start Run: ' + e);
}
}
else{
console.log('Found ' + threads.length + 'emails marked for archival. Exiting.');
}
}
@MichaelTD83
Copy link

MichaelTD83 commented Mar 11, 2022

Thanks for this code, I've been trying to automate stuff in Gmail by age of email for a while and this is the only thing that works and wasn't too difficult. For some reason I couldn't comment on the Pixel Bakery page. Anyways, I had an addons that I figured I would post here to help anyone else that finds this

I wanted to set mine up to cover multiple subfolders within a folder and this is what I came up with that works. I usually use R so this took me a little bit to figure out, let me know what you think.

function autoDelete() {
  console.log('Started autoDelete run.');
  var delayDays = 60;
  var maxDate = new Date();
  maxDate.setDate(maxDate.getDate() - delayDays );

////////////////////////////////////////////////////////////////////////////////////////////////
  var label = GmailApp.getUserLabelByName("Folder/Subfolder1");
  var labelName = label.getName()
  var threads = label.getThreads();
    if(threads.length > 0){
      console.log('Found ' + threads.length + ' emails in ' + labelName);
    var counter = 0;
    try {
      for(var i =0; i < threads.length; i++){
        if (threads[i].getLastMessageDate()<maxDate){ 
          Logger.log('deleted email: ' + threads[i].getFirstMessageSubject());
          threads[i].markUnimportant();
          threads[i].markRead();
          threads[i].moveToTrash();
          counter++;
        }
      }
       console.log('Successfully moved ' + counter + ' emails to the trash.');
    }
    catch(e){
      console.error('Could Not Start Run: ' + e);
    }
  }
  else{
    console.log('Found ' + threads.length + ' emails marked for deletion in ' + labelName + '. Exiting.');
  } 

////////////////////////////////////////////////////////////////////////////////////////////////
  var label = GmailApp.getUserLabelByName("Folder/Subfolder2");
  var labelName = label.getName()
  var threads = label.getThreads();
    if(threads.length > 0){
      console.log('Found ' + threads.length + ' emails in ' + labelName);
    var counter = 0;
    try {
      for(var i =0; i < threads.length; i++){
        if (threads[i].getLastMessageDate()<maxDate){ 
          Logger.log('deleted email: ' + threads[i].getFirstMessageSubject());
          threads[i].markUnimportant();
          threads[i].markRead();
          threads[i].moveToTrash();
          counter++;
        }
      }
       console.log('Successfully moved ' + counter + ' emails to the trash.');
    }
    catch(e){
      console.error('Could Not Start Run: ' + e);
    }
  }
  else{
    console.log('Found ' + threads.length + ' emails marked for deletion in ' + labelName + '. Exiting.');
  }
/// etc......
}

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