Skip to content

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 ="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 ?



On my side I was using this


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

Doc google (en):

Post (fr) :

Full sample script(en):

EDIT: Here is a solution to remove permanently a thread

Use Gmail.Users.Threads.remove(mymail,; 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 :

function removeMyTest() {
  var mymail = "";
  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.snippet);
        if (permanentlyRemoveMyLabel) {
          Logger.log('id: %s snippet: %s REMOVED',, thread.snippet);
    pageToken = threadList.nextPageToken;
  } while (pageToken);