11

Gmail automatically in 30 days, deletes all the emails trashed by the user. Is there any way to change this default behaviour to 7 days?

3 Answers 3

7

No, there's no way to change the default Gmail trash behavior. That includes making the duration shorter or longer.


You can create your own trash bin that does something similar, but it's kind of a pain and doesn't really do what you want. You'd have to add a custom label other than clicking the delete button every time (Via the "Labels" button then your custom trash label).

Then you'd have to create a filter (one time; under "Settings" -> "Filters and Blocked Addresses" -> "Create new filter") that "Has the words" label:custom-trash older_than:7d with the action Delete it ("Create filter with this search" -> "Okay" -> "Delete it").

But this only sends it to Gmail's trash.


To automate deletion of the trash at this point would require a userscript to open up the trash folder (using document.querySelector('[href="https://mail.google.com/mail/u/0/#trash"]').click()), clicking the "Empty trash now" button (using document.querySelector('.ya span').click()), then clicking the "Okay" button (using document.querySelector('.Kj-JD-Jl button').click()). But even still you'd have to run it with the page open, which kind of makes the filter useless...


TL;DR: No, it's not possible.

7

Yes, it is possible but it requires you to create a custom Google Apps Script. Here is how to do this, in a nutshell:

  • If you are logged in to Gmail, you can go to https://script.google.com/ and create a new Script Project .
  • Open that project and create a new script file. Put your script in it (see below for an example)
  • You will have to enable the Gmail API. In the new script editor (as of December 2020), the left panel has an option Services +. Click the + to add a service, select Gmail then click the Add button. The Gmail service is now automatically added to the default Google Cloud project backing your Apps Script project.
  • Finally, you will have to create at least one trigger that will call your script at whatever interval you choose (hourly, daily, etc), or based on some other event that you choose.

Here is a Google Apps Script to delete unwanted emails from various labels (folders), which I created based on other sources online. This code is provided without any warranty whatsoever--use at your own risk. Misuse of the Gmail.Users.Threads.remove() function could result in the permament loss of important emails.

/* Delete all emails from the given email address in the given label. */
function deleteEmailsFrom(fromEmailAddress, inLabel) {
  
    var queryEmailsToDelete = "from:" + fromEmailAddress + " in:" + inLabel;
    var permanentlyDeleteThreads = true;
    var pageToken;
  
    do {
        var threadList = Gmail.Users.Threads.list('me', {
            q: queryEmailsToDelete,
            pageToken: pageToken
        });
      
        if (threadList.threads && threadList.threads.length > 0) {
        
            threadList.threads.forEach(function(thread) {
                Logger.log('About to delete this email thread: id: %s, snippet: %s', thread.id, thread.snippet);
                if (permanentlyDeleteThreads) {
                    Gmail.Users.Threads.remove('me', thread.id);
                    Logger.log('Deleted this email thread: id: %s, snippet: %s', thread.id, thread.snippet);
                }
            });
        
        }
      
        pageToken = threadList.nextPageToken;
   } while (pageToken);
}


/* Delete all emails from the given email address in both Spam and Trash folders. */
function deleteSpamOrTrashEmailsFromAddress(fromEmailAddress) {
  deleteEmailsFrom(fromEmailAddress, "trash");
  deleteEmailsFrom(fromEmailAddress, "spam");
}

function deleteUnwantedSpamOrTrashEmails() {
  deleteSpamOrTrashEmailsFromAddress("[email protected]");
  deleteSpamOrTrashEmailsFromAddress("[email protected]");
  deleteSpamOrTrashEmailsFromAddress("[email protected]");
}
4
  • 1
    This is awesome, but I still didn't get how to deploy this stuff. It works if I run it manually, but how to make it run, for instance, every minute
    – d.k
    Commented May 6, 2021 at 18:10
  • 2
    @DmitryKoroliov You can add triggers. Open your script in the Script Editor, and check on the left pane there should be a "Triggers" view next to the currently selected "Editor" view. In the "Triggers" view, click on the "Add Trigger" button (bottom right), then set "Event Source" to "Time Driven". From there you can select the frequency of occurrence. You can also of course select which method is run. Cheers!
    – Pi Da
    Commented May 7, 2021 at 20:15
  • Do I need to make any changes in variables like fromEmailAddress and inLabel? (I have no idea about JS)
    – user279261
    Commented Aug 2, 2021 at 9:41
  • @Shub, If you want to use the sample script in my answer, there are two simple customizations you can make: (1) change the three lines "deleteSpamOrTrashEmailsFromAddress(....);" to contain the senders whose emails you want to delete (one per line); and (2) change the two lines "deleteEmailsFrom(fromEmailAddress, ....);" to contain the email folder name(s) that you want to delete these emails from (one per line). Then, as per instructions, create a trigger that calls the function deleteUnwantedSpamOrTrashEmails() every so often in order to delete these unwanted emails. Hope this helps.
    – Pi Da
    Commented Aug 2, 2021 at 14:04
0

Here is a Google Apps Script I created and have running on a daily basis. You can customize your Gmail trash to retain emails as long as you would like.

//INSTRUCTIONS
// 1. Create two lables in Gmail: SYSTEM/reset & SYSTEM/moved
// 2. Set how long you want to retain mail in trash on lines 31-34 (5m = 5 months)
// 3. Setup a trigger to run this script at least as many times a day as emails you expect to accumulate in your trash over the time you selected (1 trigger a day = preserve up to 1000 emails in trash; 2 triggers a day = preserve up to 2000 emails in trash) 

//HOW IT WORKS
// Deleted Emails are moved out of your trash and immediately back in, thus restarting the 30 day deletion time
// Labels are used to keep track of which emails have been moved

//WHY DAILY TRIGGERS ARE NEEEDED
// Google limits script to edit up to 100 emails at a time 
    
function extendTrashTime() {
    
  function move(moved_label, empty_threads) {
    GmailApp.moveThreadsToArchive(empty_threads);
    GmailApp.moveThreadsToTrash(empty_threads);
    moved_label.addToThreads(empty_threads);
  }
  function reset(reset_label, moved_label, moved_threads) {
    reset_label.addToThreads(moved_threads);
    moved_label.removeFromThreads(moved_threads);
  }
  function empty(reset_label, reset_threads) {
    reset_label.removeFromThreads(reset_threads);
  }
  let moved_label = GmailApp.getUserLabelByName('SYSTEM/moved');
  let reset_label = GmailApp.getUserLabelByName('SYSTEM/reset');
  
// CHANGE TIME TO RETAIN MAIL IN THE THREE LINES BELOW
  let moved_threads = GmailApp.search('in:trash newer_than:5m -label:SYSTEM/ignore label:SYSTEM/moved', 0, 100);   
  let reset_threads = GmailApp.search('in:trash newer_than:5m -label:SYSTEM/ignore label:SYSTEM/reset', 0, 100);
  let empty_threads = GmailApp.search('in:trash newer_than:5m -label:SYSTEM/ignore -label:SYSTEM/moved -label:SYSTEM/reset', 0, 100);
  
  let moved_total = moved_threads.length;
  let reset_total = reset_threads.length;
  let empty_total = empty_threads.length;
  
  Logger.log('moved_total', moved_total);
  Logger.log('reset_total', reset_total);
  Logger.log('empty_total', empty_total);
  
  if (moved_total >= 0 && reset_total === 0 && empty_total > 0) {
    Logger.log('Phase 1: move emails & label with moved');
    move(moved_label, empty_threads);
  } else if (moved_total > 0 && reset_total >= 0) {
    Logger.log('Phase 2: remove moved label and replace with reset label');
    reset(reset_label, moved_label, moved_threads);
  } else if (moved_total === 0 && reset_total > 0) {
    Logger.log('Phase 3: remove reset label');
    empty(reset_label, reset_threads);
  } else {
    Logger.log('No results for empty_threads query');
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.