Have you been experiencing general slowness in Apple’s Mail application, particularly when switching between mailboxes? Do you have some Smart Mailboxes that show incorrect message counts, or contain “ghost” messages that don’t exist any longer in a real mailbox? If so, the following solution might resolve the issue…then again, it might not.
Before you proceed, please heed the following warning: a very small number of people have reported that this fix caused them to lose a number of e-mail messages. As such, you’re on your own if you want to try this fix, and it is highly recommended that you back up your e-mail before proceeding. To do so, copy your user’s Library -> Mail folder to another location. (Make sure you quit Mail first.) All done? Good. Now, if you try this and something goes wrong, you can just replace the original folder and be back up and running in no time.
Mail uses a sqlite database (version 3) to store information about your messages—everything except the message content, basically, is kept in the database. To make access to the records in the database as fast as possible, the data is indexed. This index can be found in the file named Envelope Index in your user’s Library -> Mail folder. Over time, as you receive, delete, and move messages around, this index can become large and a bit cluttered with references to messages that no longer exist. As a result of these changes, Mail may start to exhibit slow downs, ghost messages, or other odd message-related behavior. The fix, then, is to rebuild the database’s index file.
You can do this from within Mail using the Mailbox -> Rebuild command, but you’ll have to do so one mailbox at a time—the menu option is grayed out if you have more than one selected. For me, with 50 or so sorting folders, that’s not a good option. Instead, you can use one of two methods to rebuild the index.
The Brute Force solution
This solution forces Mail to build a completely new Envelope Index from scratch. It’s not my preferred solution, but it will ensure that you’re starting with a completely clean index file.
Quit Mail, and in the Finder, navigate to your user’s Libary -> Mail folder, and then move the file named Envelope Index out to the desktop. When you relaunch Mail, it will tell you it needs to import your messages. Click Continue and then just wait—it took about a minute to import 9,900 messages on my MacBook Pro. When the import is done, you should be looking at Mail just as it was before—but with any luck it will be running more quickly, and any odd message-related issues you were having should be resolved.
The Terminal solution
Open Terminal and type this command:
sqlite3 ~/Library/Mail/Envelope\ Index vacuum index;
When you press Return, nothing will seem to happen. Just wait, though; you’ll get Terminal’s prompt back relatively quickly. Launch Mail, and you won’t be asked to reimport your messages, because the index itself still exists; it’s just been cleaned up. There’s a lot more detail on this procedure, as well as a number of comments on its effectiveness, in this post on the Hawk Wings Mail.app blog.
When you clean the index in this manner, any locally-cached messages and attachments from IMAP accounts will be dumped and downloaded again from the server. Typically, this would be any IMAP messages that you leave in your inbox; if you drag them to a Mail folder you’ve created, then the message and attachments are downloaded and stored locally. So after rebuilding your index, access to messages (and attachments in particular) may be slow at first, as Mail goes about downloading the previously-cached attachments. If you’ve got a lot of sizable attachments on an IMAP account, you’ll really only want to try this solution on a fast Internet connection.
I’ve never lost any mail doing this—really, you shouldn’t, as you’re just rebuilding an index file—and the first time I ran it, it made a notable difference in the time required to switch between folders. What used to take a second or so became nearly instantaneous. But please, if you’re going to try this, make sure you have a good backup before proceeding.