Automatically Unsubscribe from Newsletters in Gmail

Published in: Gmail - Google Apps Script

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


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

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube