finding unlabeled messages in Gmail

During my efforts on setting up backups for my gmail email, I decided that the approach I wanted to take was to simply label everything and then archive it. That way I can grab all the labels, which look like folders via IMAP. One thing I wanted to avoid was a lot of duplicates from the various Gmail internal folders and so I excluded them. That meant I had to make sure that all email was labeled, but much to my shock there is no way to search for all unlabled email as indicated in this Google Groups thread and this Gmail support forum thread.

There is one suggestion on how to find all unlabelled messages and it’s basically building a string that excludes all the labels. Whatever is left will be the messages that don’t have a label.

I decided that I could write a script that would at least build that search string for me to paste into Gmail.

I chose IMAPFilter for this purpose and crafted the following script for it:


gmail = IMAP {
server = 'imap.gmail.com',
username = 'USERNAME',
password = 'PASSWORD',
port = 993,
ssl = 'ssl3'
}
print(os.setlocale("C"))
function padzero(s, count)
return string.rep("0", count-string.len(s)) .. s
end
function join(delimiter,list)
local len = #list
if len == 0 then
return ""
end
local string = list[1]
for i = 2, len do
string = string .. delimiter .. list[i]
end
return string
end
mailboxes, folders = gmail:list_all('','*')
unlabeled = "-("
labels = {}
for i,mailbox in ipairs(mailboxes) do
mailbox = string.gsub(mailbox, " ", "-")
mailbox = "label:" .. mailbox
table.insert(labels, mailbox)
-- print ('"', mailbox, '"')
end
labelstring = join(" OR ", labels)
print ("-(" .. labelstring .. " OR in:chat)")

Save that script as gmail-unlabeled.lua and run imapfilter as follows


imapfilter -c gmail-unlabeled.lua

and you’ll end up with something like this:

-(label:2004 OR label:2005 OR label:2006 OR label:2007 OR label:2008 OR label:2009 OR label:2010 OR label:Accounts OR label:Receipts ......

Now you can paste that into the web interface and find all of the unlabeled messages.

Stop back in a while and I’ll share my approach to backup up my Gmail.

\\@matthias

5 Replies to “finding unlabeled messages in Gmail”

  1. Your script really has potential to resolve the issue with gmail’s odd inability to identify unlabeled emails, except I’ve tested your script and it only collects the labels from the top level. It doesn’t seem to drill down into sub-labels which use “/” to create a hierarchy. For example, the script can find these:

    label1, label2, label3

    … but not child sub-labels such as:

    label1/sublabel1, label1/sublabel2, label1/sublabel3, label2/sublabel1, etc.

    Is this a limitation of IMAPfilter to use the “/” or perhaps achievable with an adjustment to a line within your script?

    In any event, thanks for posting your work.

  2. Your script really has potential to resolve the issue with gmail’s odd inability to identify unlabeled emails, except I’ve tested your script and it only collects the labels from the top level. It doesn’t seem to drill down into sub-labels which use “/” to create a hierarchy. For example, the script can find these:

    label1, label2, label3

    … but not child sub-labels such as:

    label1/sublabel1, label1/sublabel2, label1/sublabel3, label2/sublabel1, etc.

    Is this a limitation of IMAPfilter to use the “/” or perhaps achievable with an adjustment to a line within your script?

    In any event, thanks for posting your work.

Leave a Reply

Your email address will not be published. Required fields are marked *