Search code examples
javascriptjavagmailgmail-api

Script to permenantly delete my emails with Google Script


How does this Gmail.Users.Messages.remove(userId, id) work? What is the ID of the email and is it the right function to permanently delete an email?

In my case I want to delete all my sent emails instantly and definitely. Here's some code I took from someone, only with a different label:

function myFunction() {
  var threads = GmailApp.search("in:sent label:Inbox");
  var me = Session.getActiveUser().getEmail();
    for (var i = 0; i < threads.length; i++) {
      Gmail.Users.Messages.remove(me, threads[i]);
    }

}

Is this in anyway correct and could anyone help me please?

Edit: I modified my code but it's still not working ,I still can't figure out how to use the function remove, here's it:

function myFunction() {
  
  var me = Session.getActiveUser().getEmail();
  var thread = Gmail.Users.Threads.list(me);
  for (var i = 0; i < 1000; i++) {
        Gmail.Users.Threads.remove(me, thread);
        thread = Gmail.Users.Threads.list(me).nextPageToken;
     
  }

}

Once the code is working , I'll put a trigger to run the function every minute. So that my Sent folder is always empty.


Solution

  • On my side I was using this

     thread.moveToTrash();
    

    (Agree that trash is maybe not what you expect..)

    Doc google (en): https://developers.google.com/apps-script/reference/gmail/gmail-thread#movetotrash

    Post (fr) : http://curiositedevie.blogspot.be/2015/08/gmail-gestion-de-vos-emails-aux-petits.html?m=1

    Full sample script(en): https://github.com/boly38/script.google.com/blob/master/organizeEmail.gs


    EDIT: Here is a solution to remove permanently a thread

    Use Gmail.Users.Threads.remove(mymail, thread.id); like in the sample under.

    How To use the sample :

    • Update your gmail address (at the first execution you will be asked for the related authorizations).
    • I strongly recommand to play first time using permanentlyRemoveMyLabel = false. In this case, this sample will display (and keep) the message with label:mytest
    • Once done, set permanentlyRemoveMyLabel to true. In this case, this sample will display and permanently remove the message with label:mytest.

    Sample for https://script.google.com/ :

    function removeMyTest() {
      var mymail = "[email protected]";
      var mylabel = "mytest";
      var permanentlyRemoveMyLabel = false;
      var pageToken;
      do {
        var threadList = Gmail.Users.Threads.list('me', {
          q: 'label:' + mylabel,
          pageToken: pageToken
        });
        if (threadList.threads && threadList.threads.length > 0) {
          threadList.threads.forEach(function(thread) {
            Logger.log('id: %s snippet: %s', thread.id, thread.snippet);
            if (permanentlyRemoveMyLabel) {
              Gmail.Users.Threads.remove(mymail, thread.id);
              Logger.log('id: %s snippet: %s REMOVED', thread.id, thread.snippet);
            }
          });
        }
        pageToken = threadList.nextPageToken;
      } while (pageToken);
    }