Google Apps Script - Gmail, delete forever e-mails in trash with specific label

前端 未结 5 976
囚心锁ツ
囚心锁ツ 2020-12-09 11:19

I\'m trying to make a script that automatically deletes e-mails from a certain sender immediately and permanently, as Gmail only allows for a filter which sends an e-mail to

相关标签:
5条回答
  • 2020-12-09 11:30

    @karan's answer already points to the solution which worked for me, but being inexperienced / not-a-professional-developer, it took me a little work to translate it into a working solution to the original question. Here's a concise description of the steps I used to perform this task:

    1. Create the following function in my script:

      function deleteForever(userId, labelName) {
        var threads = GmailApp.search("in:trash label:" + labelName);
        for (var i = 0; i < threads.length; i++) {
          Gmail.Users.Messages.remove(userId, threads[i].getId());
        }
      }
      
    2. To enable advanced services for this script, locate Resources on the menu, and select Advanced Google services...

    3. Enable Gmail API on the list.

    4. Before selecting OK, click on the Google Developers Console link. Search for gmail, and enable the service there as well.

    5. Done, select OK; the function should now work. (Comment: as mentioned in the link @karan provided, one can use "me" for userID, or alternatively provide one's Gmail address: "<address>@gmail.com".)

    (Steps to enable advanced services for my script are based on Google's guide here.)

    0 讨论(0)
  • 2020-12-09 11:33

    If it helps someone, it can be done using the advanced services.

    https://developers.google.com/gmail/api/v1/reference/users/messages/delete

    The method in advanced services is

    Gmail.Users.Messages.remove(userId, id)

    0 讨论(0)
  • 2020-12-09 11:36

    This script works for Google Apps Script. You have to just connect and auth services together.

    function myFunction() {
    
      var labelName = "deleteForever"
    
      var threads = GmailApp.search("in:trash label:" + labelName);
      for (var i = 0; i < threads.length; i++) { 
        Gmail.Users.Messages.remove('me', threads[i].getId());
      }
    }
    
    0 讨论(0)
  • 2020-12-09 11:37

    It is not possible, by design, to delete an email permanently using GmailApp.

    0 讨论(0)
  • 2020-12-09 11:50

    try this

    function delete_all_mail(){
    var myspreadsheet = SpreadsheetApp.openById('1AG1fZ9BuS8***********');
    var mysheet = myspreadsheet.getSheets()[0];
    //0~500
    var threads = GmailApp.getInboxThreads(0 , 500);
      for(var i = 0; i < threads.length; i++)
      {
       threads[i].moveToTrash();
      }
    }
    
    0 讨论(0)
提交回复
热议问题