- 
      
 - 
        
Save zephyrer/438c24c6bd3d4ea3d358712719f5a814 to your computer and use it in GitHub Desktop.  
    Google Apps script to cleanup messages from GMail.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Inspired in http://www.johneday.com/422/time-based-gmail-filters-with-google-apps-script | |
| // Deletes old marked conversations | |
| function cleanUp() { | |
| var delayDays = 5; // # of days before messages are moved to trash | |
| var label = "Delete me"; // label to identify the messages | |
| var maxDate = new Date(Date.now() - delayDays * 24 * 60 * 60 * 1000); | |
| var userLabel = GmailApp.getUserLabelByName(label); | |
| if (!userLabel) { | |
| return; | |
| } | |
| var threads = userLabel.getThreads(); | |
| threads.forEach(function (thread) { | |
| if (thread.getLastMessageDate() < maxDate) { | |
| thread.moveToTrash(); | |
| } | |
| }); | |
| } | |
| // Deletes old conversations in selected categories | |
| function batchDeletePromotions() { | |
| var categories = ["social", "promotions", "forums", "updates"]; | |
| categories.forEach(function (category) { | |
| GmailApp.moveThreadsToTrash(GmailApp.search('label:inbox category:' + category + ' older_than:2d')); | |
| }); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment