Script to permenantly delete my emails with Google Script

北城余情 提交于 2019-12-08 18:26:24

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 = "myemailhere@gmail.com";
  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);
}
AL.

I've done some digging in the docs. With the result you want, I think what you are looking for is User.messages.delete. As mentioned in the docs:

Immediately and permanently deletes the specified message. This operation cannot be undone.

As per the Gmail.Users.Messages.remove(), I'm not sure with this, but I think that its an old implementation, because I looked around the community and found this answer providing it as the snippet for permanently deleting an email/message. I'd stick to what the current docs says though.

Hope this helps. Good luck. :)

This will remove all of the conversations from the sent folder. NB If someone sent you a message and you responded that would be part of the same conversation, and I think that the incoming message would be deleted as well. This may not be what you want.

function DeleteByLabel() {

  var label="sent"
  var threads = GmailApp.search("in:" +label);
  var me = Session.getActiveUser().getEmail();
  Logger.log("Found " + threads.length + " threads with label " + label);

  for (var i = threads.length -1; i >=0; i--) {
     var thisid=threads[i].getId();
     Logger.log("Removing thread " +i + ' of ' + threads.length + " from " + label + " with ID = " + thisid);

    try {
        Gmail.Users.Threads.remove(me, thisid);
    }
    catch(err) {
     Logger.log("Error " + err.message + " when removing thread with ID " +thisid);
    };
}

}

Tried it on my "Drafts" folder and it got rid of those Ok.

NB You will have to enable the appropriate Gmail API when you run it.

This used to work until mid-Feb 2017.

I used this to empty my trash

  `var threads = GmailApp.getTrashThreads(0, 100);
  for (var i = threads.length -1; i >=0; i--) {
        Logger.log("Removing thread " +i + ' of ' + threads.length + " from Trash");
    try {
    Gmail.Users.Messages.remove(me, threads[i].getId());
    } catch(err) {
    Logger.log("Error " + err.message);
    };
  }
  Logger.log("Finished removing " + threads.length + " threads from Trash.");`

It has now stopped working and is giving these errors:

`17-02-25 02:10:53:510 PST] Removing thread 1 of 2 from Trash
[17-02-25 02:10:53:620 PST] Error Not Found
[17-02-25 02:10:53:621 PST] Removing thread 0 of 2 from Trash
[17-02-25 02:10:53:763 PST] Error Not Found
[17-02-25 02:10:53:764 PST] Finished removing 2 threads from Trash.`

It seems that there has been a change to the functionality recently.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!