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

The Cheapest NBN 50 Plans

Here are the cheapest plans available for Australia’s most popular NBN speed tier.

At Lifehacker, we independently select and write about stuff we love and think you'll like too. We have affiliate and advertising partnerships, which means we may collect a share of sales or other compensation from the links on this page. BTW – prices are accurate and items in stock at the time of posting.


Leave a Reply