Script to permenantly delete my emails with Google Script

孤街浪徒 提交于 2019-12-08 04:50:02

问题


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.

Any suggestions please ?


回答1:


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);
}



回答2:


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. :)




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/36377391/script-to-permenantly-delete-my-emails-with-google-script

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