Automatically Unsubscribe from Newsletters in Gmail

The bulk email messages in Gmail have an “unsubscribe” link that you can click to remove your email address from the mailing list. However, if you wish to unsubscribe from multiple email newsletters in one go, you can use the Gmail Unsubscriber script.

Apply the label “Unsubscribe” to all the emails from which you wish to unsubscribe and call this apps script. It extracts the unsubscribe link from the raw message header and fetches the link to unsubscribe you. Press Cmd+Enter to see the list of all mailing lists from which you have been unsubscribed.

Joshua Peak has done the groundwork but it only works if the email message contains the unsubscribe link in the List Unsubscribe header.

function main() {
  var label = GmailApp.getUserLabelByName('Unsubscribe');
  var threads = label.getThreads();

  threads.forEach(function (thread) {
    var message = thread.getMessages()[0];
    var value = message.getRawContent().match(/^List-Unsubscribe: ((.|\r\n\s)+)\r\n/m)[1];

    if (value) {
      var url = value.match(/<(https?:\/\/[^>]+)>/)[1];
      if (url) {
        var status = UrlFetchApp.fetch(url).getResponseCode();
        Logger.log('Unsubscribe ' + status + ' ' + url);
      }
    }

    thread.removeLabel(label);
  });
}

I extended this to unsubscribe from mailing lists where the link may be in the message body or messages that may require your to unsubscribe by sending an email to a specific email address.

function Gmail_Unsubscribe() {
  var threads = GmailApp.search('label:Unsubscribe');

  for (var t in threads) {
    var message = threads[t].getMessages()[0];

    var raw = message.getRawContent();

    // Search for the List Unsubscribe header in the Email Header
    var urls = raw.match(/^list\-unsubscribe:(.|\r\n\s)+<(https?:\/\/[^>]+)>/im);

    // thanks josh/list-unsubscribe @github

    if (urls) {
      // Click the unsubscribe link
      UrlFetchApp.fetch(urls[2], { muteHttpExceptions: true });
    } else {
      // Find the unsubscribe email
      urls = raw.match(/^list\-unsubscribe:(.|\r\n\s)+<mailto:([^>]+)>/im);

      if (urls) {
        // Send blank email to unsubscribe
        GmailApp.sendEmail(urls[2], 'Unsubscribe', 'Unsubscribe');
      } else {
        // Get the HTML of the email
        var body = message.getBody().replace(/\s/g, '');

        // Regex to find all hyperlinks
        var hrefs = new RegExp(/<a[^>]*href=["'](https?:\/\/[^"']+)["'][^>]*>(.*?)<\/a>/gi);

        // Iterate through all hyperlinks inside the message
        while ((urls = hrefs.exec(body))) {
          // Does the anchor text or hyperlink contain words like unusbcribe or optout
          if (
            urls[1].match(/unsubscribe|optout|opt\-out|remove/i) ||
            urls[2].match(/unsubscribe|optout|opt\-out|remove/i)
          ) {
            // Click the unsubscribe link
            UrlFetchApp.fetch(urls[1], { muteHttpExceptions: true });
            break;
          }
        }
      }
    }
  }
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻