Search code examples
gmailgoogle-apps

Recursively delete emails and labels under a given label in GMail


We recently migrated to Google Apps and one of our users' data has a few thousands labels (created in Outlook) organized in a tree-like fashion. Under a few of these labels (i.e. a few hundreds), are emails that need to be deleted along with said labels. In GMail, it is not possible to see emails recursively when selecting a parent label, and the only solution is going one-by-one, selecting the child label, selecting all emails, delete them, then delete the label... and repeat this on and on for all the hundreds labels.

Is there some way to automate this? I have looked for a script, or app, but none does this. I could invest a few hours of my time to come up with a home-brewed solution, but if there's such a tool already, I'd like to know.

Can this be done, somehow, other than by implementing it myself?


Solution

  • How many user-created labels other than sublabels of the parent label does the user have? If it's not all that many you could try a Gmail search like:

    has:userlabels -label:otherlabel1 -label:otherlabel2 -label:otherlabel3
    

    assuming you replaced otherlabel1, otherlabel2, otherlabel3, etc with any labels the user has that aren't sublabels of the parent, this would return all messages that ARE under the parent label and you could then mass delete those messages in one go.