Thursday, 5 September 2013

Recovering accidentally deleted emails in imap gmail account

In a similar fashion to what happened with my calendars about a year ago, today I ran into a problem  with Gmail, losing all my fourteen thousand plus emails. I couldn't find any solution to my problem on the Internet, but I'm sure I'm not the only one who will run into this problem. So after figuring out how to fix it, I decided to outline the steps I took to restore all my emails, hoping it might help someone who faces a similar situation.

(If you don't want to read about how I lost my emails, jump down to “The solution”  below.)

The problem

What happened was this: I have my Gmail IMAP account linked to Mail in Mac OS X according to the method initially proposed by Joe Kissel. One excellent feature of this method is that the “All Mail” label in Gmail does not appear as a folder in Mail on my Mac (I have never been able to adapt to Google's approach to email :-)

Everything was working fine until I installed on my iPod Dropbox's Mailbox program. I tried it first on a test Gmail account, and loved their interface (swipe right to archive, long swipe right to delete was brilliant!). The only caveat was that Mailbox only works if you enable your “All Mail” label in Gmail. “Oh well”, I thought, “let's give it a try”.

I tried it, but a few days later got fed up with having that massive “All Mail” folder on my Mac, with a copy of every single email I've sent, and every email that I keep in other folders (or “labels” in Gmail). In a moment of  “mental off-line”, I opened Mail, selected all the emails in “All Mail”, and hit delete.

Two seconds later I woke up, and realized my mistake: instead of deleting emails on my Mac, I should have modified the status of “All Mail” in Gmail's web-site (Gmails labels work like tags, so I only needed to hide “All Mail” again from the web interface). So I hit the “Stop” button in Mail's Activity Window (which, as you probably know, takes about 237 hours to actually stop anything it's doing!).

And just as that moment, my Internet connection fell (something not so uncommon here in the interior of Brazil). “No problem”, I thought; “I'll sort it all out later”. And in blissful ignorance, I let the matter rest.

When I managed to connect again, imagine my dismay when I saw that my “All Mail” folder was completely empty! Every single one of those fourteen thousand odd emails permanently deleted! (As an aside, I don't know why this happened, but there was no doubting the fact that it did happen!)

After the initial shock wore off, I remembered Time Machine (Steve Job's version, not H. G. Wells!), and breathed a sigh of relief.

The difficulty

The solution seemed very simple. Since I’m running Mac OS X Lion, all I should need would be to fire up Time Machine and restore my emails, right?

Wrong. Or rather, partially right. I entered Time Machine, restored my ~user/Library/Mail/V2 folder, fired up Mail (while off-line), and sure enough, everything was back to normal again. But then I went on-line, and in five seconds flat, Mail synchronized it's folders with Gmail's online labels, and all my mails vanished again!

“Back to marker zero”, as they say there in Brazil. Problem not solved, and patience running out!

The solution

Anyway, to cut a long story short, I finally got all my emails restored and properly synchronized with Gmail. If you have the same problem, here’s an easy-to-follow step-by-step guide to getting Mail back to the state it was in at some time in the past (assuming, of course, that you have been using Time Machine!).
  1. Quit Mail.
  2. Navigate to the “Library” folder inside your home folder. This folder is hidden by default, so if you can’t find it, select the “Go” menu in the Finder, then choose the “Go to Folder ...”. In the dialog box that will appear, type in: ~yourusername/Library (where “yourusername” should be substituted for your user name, of course).
  3. Once in your “Library” folder, open the “Mail” folder, then the “V2” folder (it should be the only folder inside the “Mail” folder).
  4. Now enter Time Machine. [You can enter Time Machine without leaving the “V2” folder using one of the following options: (1) through the Time Machine menu-bar icon, or (2) using Spotlight (hit “command” + “space-bar” to bring up Spotlight's search box, and search for Time Machine).]
  5. Go back to the date you wish to grab your emails from (in my case, I went back one day in Time Machine).
  6. Click “Restore” (Time Machine will notify you that the files you are trying to restore already exist; choose “Replace”).
  7. Once Time Machine has restored your files, make sure you are not connected to the Internet and fire up Mail again. You can accept any prompt to import all your messages.
  8. Now you need to archive all the emails you want to keep. For every folder where you lost emails (Inbox, Sent, All Mail and any other folder you have linked to your Gmail account) you will need to: (a) select the folder, (b) from the “Mailbox” menu, choose “Export Mailbox”, and (c) save your archive somewhere handy (Desktop, for example).
  9. Repeat step 8 above for every folder you have in Mail (at least, the ones that have emails you want to preserve!).
  10. Only after Mail has finished archiving all your folders (you'll be sure it's finished when it allows you to Quit without alerting you to any unfinished process), you can go online again, and allow Mail to synchronize with Gmail.
  11. As you should expect, all your Mail will disappear. Don't worry, though; that's why we archived it all.
  12. Begin re-importing all the folders you exported in step 8 (choose “Import Mailboxes” from the “File menu”, select the archives you created in step 8, and wait for Mail to finish). It will add all your folders to the left sidebar of Mail's main window, underneath an “Imported” label.
  13. You should now a copy of each of your folders — one empty, and an imported version with all your original emails. For example, your Gmail “Inbox” will be empty (it will only have copies of emails you have received since your problem began), but you'll have another folder named “Inbox” below the “Imported” label, where you'll find all your old emails from the Inbox folder.
  14. Now comes the slow part (especially if you have a lot of email messages!). For every archive you created, you will need to: (a) select it in Mail (click on the folder name on the left sidebar under the “Imported” label); (b) select all the messages in that folder; (c) from the “Message” menu, select “Move to” and choose the original folder as your destination. For example, to restore the “Inbox” you will: (a) select  the folder named “Inbox” below the “Imported” label on the left sidebar of Mail's main window; (b) select all messages in that folder (by hitting command + “a”); and (c) move all these messages to the folder named “Inbox” in your Gmail account (which you should find right at the top of the left sidebar in Mail's main window).
  15. Be patient; it can take some time to get everything synchronized between Mail and Gmail, especially if your Internet connection isn't 100%. To give you a rough idea, I started restoring my emails on Tuesday, and finished on Wednesday (sleeping in between, of course :-), doing one folder at a time. In theory, you don't need to wait for Mail to finish updating one folder before you throw another one at it, but it's safer to take things easy and let it finish one job before giving it another.
  16. Once all your folders are restored, you can safely delete your imported archives from Mail.
And that’s it. If you need any extra help, let me know in the comments below. Cheers.

© Ben Ulster