How to Clean Up and Rebuild Apple Mail Data

Question:

Joe writes: My Apple Mail mailboxes appear to have become corrupted; when I click on a mailbox in Mail, no messages appear. Following the instructions in Mail’s help files, I used the Mailbox > Rebuild command on an individual mailbox and it solved the issue.

My problem is that I have hundreds of mailboxes and it would be too time consuming to perform this step on each box individually. Is there a way to perform the rebuild command on all my mailboxes at once?

Answer:

Mail Mail Mail!

MAIL MAIL MAIL!

There are two methods to rebuild your entire mail library, including all of your mailboxes. The first is to navigate to a folder in your user directory. In Snow Leopard, this folder is ~/Library/Mail and in Lion and later (at least through Yosemite) it’s ~/Library/Mail/V2/MailData. Inside both of these folders are files that start with “Envelope Index” that store all your mailbox data, among other things.

Quit Mail, then delete all the files in this folder that start with "Envelope Index." On Yosemite that will be three (3) files (Envelope Index, Envelope Index-shm, and Envelope Index-wal); on older versions of OS X it may be simply one file. Once deleted, relaunch Mail and the file(s) will be rebuilt, along with your mailboxes.

The second method involves using Terminal. Open up Terminal and type the following command using the correct path for your operating system. For Lion through Yosemite (and possibly future OS's), it’s:

sqlite3 ~/Library/Mail/V2/MailData/Envelope\ Index vacuum 

For Snow Leopard, the command is:

sqlite3 ~/Library/Mail/Envelope\ Index vacuum

Remember, you should only have to perform one of these methods, not both. That should solve your problem and rebuild all of your mailboxes.