Fix Gmail Label Issues With An App Script

Having issues keeping Gmail labels properly applied in an IMAP client? Lifehacker reader Bonne shares the script he used to solve the issues.

For years I've battled with the problem of getting the contents of IMAP folders to perfectly match Gmail label conversations. Google doesn't really advertise this but labels are actually applied to individual messages (not conversations or threads), which means you can have a conversation that shows up under a label in GMail but only some of the messages in it will show up under the corresponding IMAP folder.

This can be really annoying if you're trying to move your messages from one Gmail account to another as you will end up missing some. You essentially have to "reapply" all the Gmail labels to every conversation and the GUI doesn't let you do that easily so it's a real pain. But Since Google released Apps Scripts there is now a solution.

Open Google Docs and create a new spreadsheet. Click Tools -> Script Editor, then paste the following into the editor window:

function relabeller() { var labels = GmailApp.getUserLabels();

for (var i = 0; i 0; j++) { Logger.log( (j - 1) * 100 + threads.length); labels[i].addToThreads(threads); threads = labels[i].getThreads(j*100, 100); } }

Click Run -> relabeller. Follow the prompts to allow your script to modify your email (You may have to click Run -> relabeller again afterwards.) Then wait. (You can follow the progress by clicking View -> Logs.)

After the script has finished all of the messages that show up under your Gmail labels will also show up in your IMAP folders! I hope this little tip saves someone the trouble of manually reapplying all their labels. Thanks Bonne!


    Pretty cool solution!
    Is there a way to run this script based on a filter or a search in gMail? (e.g. "-in:trash has:nouserlables")
    Thanks a lot!

Join the discussion!

Trending Stories Right Now