Search code examples
google-apps-scriptgmail-api

Delete a Gmail filter using Google Apps Script


I'm trying to programmatically edit Gmail filters using the Gmail API in Google Apps Script, but it seems like the only way to do this is to delete the old filter and create a new one.

I'm stuck on the deleting part. I've read the docs, and I can get and confirm that the filter exists. e.g. this works: Logger.log(Gmail.Users.Settings.Filters.get('me','ANe1BmhOkABo1X_WLY55dIK1QhmIhPmtAiE_oQ'));

But this gives me a "Gmail.Users.Settings.Filters.delete is not a function" error: Gmail.Users.Settings.Filters.delete('me','ANe1BmhOkABo1X_WLY55dIK1QhmIhPmtAiE_oQ');

What am I missing?


Solution

  • I was testing the method Gmail.Users.Settings.Filters.delete and the reason why "Gmail.Users.Settings.Filters.remove" is not recognized is due to "delete" in App Script is a reserved word in JavaScript according to this documentation.

    enter image description here

    You can try this instead:

    function deleteEmailfilter() {
     const filter = Gmail.Users.Settings.Filters.get('me', 'filterId')
     console.log(filter)
     console.log('== Deleting filter ==')
     Gmail.Users.Settings.Filters.remove('me', 'filterId')
     console.log('==* Completed *==')
    }