Rebuild your database and speed up Mail.app

Envelope Index (see “What’s in your Mail folder?”) is an SQLite database that stores key information about your emails. Over time it can get terminally corrupted (see “The dreaded ‘your home directory is full’ message”).

It can also suffer a large number of smaller hiccups and corruptions which slow Mail down, although they don’t knock it out.

A poster on macOSXHints explains how his database stored phantom messages from an Exchange server he used over Summer.

Quitting Mail, dragging the Envelope Index to the Desktop, restarting Mail and allowing it to reindex his emails, cut its size from 200MB to 2MB. The speed increase was significant. Rob Griffiths reduced his from 25.9MB to 4.5MB.

My result was less dramatic (21.6MB -> 17.6MB) but Mail.app still feels a little more zippy.

Worth a shot for you too, perhaps? If so, first make sure you have a backup of your ~/Library/Mail folder in case something goes wrong. Dragging it out of Finder onto your Desktop will do the trick.

Tags: , , , , , , , ,

Related posts

50 Responses to “Rebuild your database and speed up Mail.app”

  1. Stuart says:

    Awesome, thanks for passing that on, took something like a quarter off my 20MB index :D

  2. Dan Ridley says:

    It’s worth noting that if you do this, Mail.app will forget any account-specific customizations you’ve made to the special folders it uses on IMAP accounts. (That is, if you’ve gone into the Mailbox menu, then selected “Use This Mailbox For”.)

    Mine grew from 6.2 MB to 9.5, but it’s noticeably faster (opening a folder with 1,951 messages in a few seconds instead of 30-45 seconds). Weird.

  3. David Chartier says:

    Incredible. My mailbox has over 13,200 messages, and mine went from 300 MB to 3.7!

  4. Tim says:

    Amazing! That’s the best result I’ve ever heard by a country mile.

    Did you get a “speed bump” as well?

  5. Sebastian says:

    Strange, this article just came in via RSS as ‘brand new’. :-P

    Be warned: This process can turn into a lenghty one! I’m using like 20 IMAP subfolders, and recreating the Envelope Index took 15 minutes for 18000 messages on my iBook 1,0 GHz. BUT: Now Mail is redownloading all messages and attachments (you will have to click through every folder to start downloading). This takes ages to complete…

    The Envelope Index file size didn’t change for me either: 6,6 MB before and after.

  6. Tim says:

    That is odd.

    Sorry to hear that there was no benefit for you. :(

  7. Graham says:

    Didn’t work for me, in fact Mail kept crashing on me for about half an hour afterwards, having to rebuild the index each time. My index is now larger and Mail is now slower at opening mail (but quicker overall). But it seems to have recovered 100 e-mails I thought I’d either lost or deleted, so that’s not a bad result overall.

    /OSX10.4.6, iBook 1Ghz

  8. Casualtek » Mac OS X 10.4 : Accélérer Mail says:

    [...] Tim Gaden, sur son blog Hawk Wings, propose une intéressante solution pour accélérer Mail, ou du moins le soigner de certains ralentissements qui le prennent, l’âge aidant. Celle-ci consiste à quitter Mail pour ensuite ouvrir le dossier ~/Bibliothèque/Mail/ et à en extraire, sur le Bureau, le fichier Envelope Index. Refermez le dossier et relancez Mail. L’application se charge automatiquement de reconstruire l’index des messages qui, au passage, maigrit considérablement. Mon fichier Envelope Index est ainsi passé de plus de 11 Mo à moins de 5 Mo. Mail se lance désormais plus vite et semble plus réactif. [...]

  9. Carlos Alberto Pinto Peixoto Bastos Santos says:

    Strange, I don’t have an envelope file in my Mail folder. Is it bad?

  10. Adam Jury says:

    I only use POP accounts — no IMAP, no wacky corporate networks, nothing beyond a few email accounts and at least six point eight bazillion messages, and the file size reduction was only 4MB, down to 20MB from 24MB, and within a few hours it had grown back up to 23MB.

  11. Tim says:

    Carlos: It’s pretty bad. Email me.

  12. Tim says:

    Hmmm…. I use only IMAP. Not sure what David Chartier uses. Perhaps the bloat is greatest in IMAP accounts?

  13. David Chartier says:

    I use 100% IMAP. I’m wondering if bloated Envelope Indexes might be a result of some bug inherent either in Mail or one of its plugins that I use. But since I am no programmer, I’d rather not make any more wild speculation than that.

  14. Tim says:

    That’s interesting. IIRC, Rob Griffiths uses IMAP too and he also posted a whopping reduction in his database after the rebuild.

    Still, following your prudent advice, I’ll refrain from any more wild speculation ;-)

  15. Nick Chapman says:

    How and where is the index stored in versions of Mail prior to the 10.4 one? (I’m assuming that the Envelope index was introduced with 10.4). I’d like to delete my index to see if it improves performance, etc., after deleting a few thousand email messages.

  16. Tim says:

    I don’t think there was an index like this in Panther Mail. It is all stored and searched in the mbox files.

    The index was part of the shift to individual files for each message in Tiger.

    I could be wrong though. It has happened before.

  17. Sasha S. says:

    Thank you a lot for an excellent tip,

    My partners laptop (400Mhz Ti Powerbook) Mail (Tiger 10.4.6.) was plagued with numerous “Message not downloaded” messages. Using this tip did bring the significan reduction in Envelope index file as well as cured all the problems related to messages apparently not localy saved (they were, but the index was wrong).

    importing 3000 messages on this machine (1 GB worth of mail) took about 1 minute.

    Thank you!

  18. Kit says:

    Surprising to hear everyone’s Envelope Indeces are so enormous; mine is 980KB.

  19. Charlie says:

    My Mail will hang on the ‘caching attachments’ process. When I stop that in the Activity Viewer the other processes continue.
    Someone the same problems?

  20. Bill says:

    My mail envelop was cut in half (7.6MB to 3.8MB) but I still have phantom mail that apparently results when I bounced some unwanted email back to the sender… the “returned mail” notice keeps appearing in my inbox. When I delete it the mail count number doesn’t change, the file disappears momentarily but reappears soonn after. Any one had this problem?

  21. Dan Ridley says:

    Bill — have you tried repairing permissions? That sounds like maybe Mail can’t delete the file.

  22. Bill says:

    Thanks for the suggestion, Dan. Great idea so I just ran disk utilities on the startup disk but the only bogus permission was for widgets… and it fixed that. Even ran through this routine and First Aid a second time just to make certain.

    Most perplexing. The files are still there… but they aren’t there. They show up in the inbox list but when deleted the number of messages tallied for the inbox remains the same.

    Where are inbox messages stored? I’ve thought about transferring everything from inbx to a holding folder, then getting rid of the plist or preferences file or whereever these incoming messages are stored and letting it rebuild… but what file hold incoming stuff? I can find mboxes for all of my folders in the users path but no mbox for inncoming mail.

    BTW, I have mail preferences set to delete messages as they are retrieved on the server… so when I go directly to our server the phantom messages are not there — they’re only visible locally on my Mail app operating under 10.4.7.

    Most perplexing…

    Bill

  23. Dan Ridley says:

    In Tiger, your messages are in ~/Library/Mail. Look for a folder starting with POP- and followed by your e-mail address — if you have multiple e-mail accounts, there will be one for each account (and if you have IMAP or .Mac accounts, you’ll see Mac- or IMAP- folders). Inside that folder will be another folder titled INBOX.mbox, inside that will be a folder called Messages, and inside that will be a bunch of .emlx folders containing your mail.

    If you’re even marginally comfortable with Terminal, you should be able to find the .emlx file for the stubborn messages with grep.
    Open TerminalType cd ~/Library/MailType grep -r "Unique Text" * where you fill in something that will identify those messages in place of Unique Text. (Include the quotes, though.)
    That’ll get you a list of files that contain that text; hopefully it will be few enough that you can narrow down to the affected messages. If not, try for some “uniquer” text.

    Once you know the actual files containing those messages, see if you can move them to the Trash from Finder (or delete them from Terminal using rm if you’re comfortable with that, but beware that rm will delete them permanently, rather than moving to the trash, so be careful).

  24. Dan Ridley says:

    Um… that looked a lot better when i was typing it. Tim, what’s up with the live preview that doesn’t actually look like the result? Do you have something somewhere telling us what HTML we’re allowed to use?

  25. Bill says:

    Thanks again for the directions, Dan. I modified your suggestion with something even easier. I moved all my inbox messages except the “phantom menace” to a new “transfer” folder. Then I quit Mail and rebooted. When I navigated to the inbox.mbox folder, the only emix messages that were left were my phantom ones. I dragged all these emix files to the trash and deleted their headers in Mail. Then I quit Mail and rebooted. When the program came back up the phantom files were gone. Yeah, baby!

    BTW, the emx “phantoms” — which were all notices of returned messages — had no content at all (nothing showing that delivery was attempted and denied and so on), just headers. Very odd… now they are gone, I hope. I’ll let you know if this “fix” reverses itself. So far so good.

    Many thanks

    Bill

  26. Jon says:

    I quit Mail.app and then used the sqlite3 command line to execute the vacuum command on the subjects table in the Envelop Index. It worked like a charm and Mail.app is much faster now.

    jon

  27. Bill says:

    Worked great for me! Went from 12MB to 5.7MB, and a noticeable speed increase.

  28. Ben says:

    Wow.

    It’s like someone just took the (noisy hard-drive grinding) parking break off of my Mail.app.

    Thanks soooo much for posting this. It’s really hard to search the net for things about apple’s mail program because the name is so generic. Any other email client has a specific name (at least until vista’s email came along, that is) that you can easily narrow down with a search engine. I’ve been dealing with a painfully slow mail.app for several months. Just kept getting worse. It shouldn’t take my system down to a crawl every other time I get an email. A dual G5 should be able to check email without a hiccup, but I was getting 2-3 minutes of beachballing when mail.app checked itself in the background and got a new tiny bit of spam with no images or attachments.

    I tried removing all but the most recent emails from my inbox thinking maybe mail.app was just bad at handling stuff…there’s no “rebuild database” option anywhere in the program that I could find, so I thought I was just going to have to live with it. This is Excellent! My envelope index file only reduced in size from 6.3 to 4.8 mb, but good grief is it faster. It’s like a new machine!

    That this can even happen tells me that something is very wrong with the self-maintenance of mail.app. It’d be nice if it could keep its own files clean, wouldn’t it?

  29. Tim says:

    Excellent news. Mine felt like new the first time I tried this too.

    It would be good if Mail offered some self-maintenaince. Good idea.

  30. Dallas says:

    I also did the sqlite vacuum command. I think that’s method’s probably a bit safer and definitely less time consuming. I had never used sqlite’s command line interface before and had to figure it out. So, for prosperity:

    Quit Mail first!

    open Terminal and then…
    cd ~/Library/Mail
    sqlite3 Envelope\ Index
    sqlite> vacuum subjects;

    it’ll churn for awhile and voila. You can then close the Terminal window or use ‘cntrl-D’ to get out of the sqlite command prompt.

    Worked for me, anyway. It’s a bit more complicated than just making Mail rebuild the whole thing from scratch, but it’s faster.

  31. Tony says:

    Just want to give a big thumbs-up to Dallas for the sqlite3 vacuum syntax and Jon for initially mentioning it.

    My ‘Envelope Index’ went from 54 to 52 Megs (no biggie) but wow! the speed at which folders open now makes me feel like I have a new Mac!

    Highly recommended and Merry Christmas all!
    Tone

  32. Steve says:

    You cut mine down from 683mb to 3. Yes, cut down by over 200 times. I have 6000 messages and it works SO snappy now. Thanks -

  33. Sean says:

    Thanks for the tip! Not a dramatic decrease in size (1.9 meg to 1.2 meg) but Mail is definitely a bit faster on its feet

  34. Gil Friend says:

    Mine just dropped from 80mb to 75mb. Switching email folders is generally zippier, though. I hope this will also make Mail less crash-prone…

  35. Gil Friend says:

    JUst discovered that 4-5 days of attachments are vanished from my Mail Downloads folder! I can only assume that it was as a result of this.

    (I backed up my Mail folder, as advised, but didn’t back up the Mail Downloads folder :-(

    Anybody got any ideas on where/whether/how to recover them?

  36. MrHen says:

    I experienced no change in sizes of anything, but Mail is notably faster.

    I did have to sort through and “reread” a handful of messages, so I suggest that everyone check and read all of their current mail so they can just select everything and mark as read after the reindex.

  37. Shaun Inman says:

    Dallas’ command line sqlite3 worked a treat for me, halving my 103 MB Envelope Index and reducing folder open time from 30+ seconds down to a mere 3 seconds (that for a folder containing 4000+ messages).

  38. Daniel Beck says:

    I’m using all POP accounts, have ~7000 messages, and my Envelope Index was 6.6MB before vacuuming, and 6.5MB after. No noticeable speed difference in Mail (though it wasn’t particularly slow to begin with).

    I’m seconding or thirding the theorizing that this problem is either an IMAP or a wacky-corporate-network thing; boring old popmail accounts don’t seem to bloat up or slow down like some of you are seeing.

  39. bpeople says:

    I tried Dallas’ way (Terminal+sqlite3) and mine dropped from 15MB to 7MB and Mail now is a bit faster.

    Thanks for the tip!

  40. Philby says:

    Wow. Thanks for this very cool tip. I just did the sqlite vacuum command, as described by Dallas, and Mail.app feels like reborn. A folder of almost 7000 messages opens in the blink of an eye… the way it is supposed to, IMHO.
    Waiting 30 (or even 5) seconds for a mail folder to open is way too long; I’ve been playing around with PowerMail and Thunderbird lately, as Mail.app was getting decidedly sluggish.

  41. Bob Monsour says:

    Wow! I was just starting to think about Mail app alternatives when I came across the sqlite3 command-line vacuum here. I have folder of archived messages, containing more than 14K messages. It was taking an excruiatingly long time to open. It just zips right open now.

    My Envelope Index went from 19.6MB to 16.6MB.

    Thanks loads Dallas!

  42. Jacob Reiff says:

    I used Dallas’ command line, worked great. Chopped my E.I. from 9.1 to 4.1, nothing screwy with read/unread msgs, and *wow* does Mail.app run faster.

  43. Yi Qiang says:

    Fantastic tip! I notice a speed up as well.

  44. Rudolf says:

    With my mailbox being 7,6 GB probably because of all the attachements, I thought I give this a try. Now the mail directory is only 1 MB…

    How can this be? We use an Exchange mailserver at the office, would that be the reason? Messages are all there, including the attachements… and it’s quite a lot quicker !

    Thanks for sharing this trick, although I still have no qlue how it works…

  45. Jim says:

    Okay - that’s improved the overall performance, but I have a real problem with new mails: it takes over 1 minute 20 seconds to create a new mail! This was happening before, so I know it’s nothing to do with the tips here (they’ve helped-out with other speed issues dramatically) but does anyone have a tip for sluggish “new mail” creation?

  46. Clemens says:

    using sqlite really did the trick. My envelope file was just 13MB so I thought not much would happen. It’s now 2.7MB and the start up speed is incredible. Absolutely mind blowing quick.

    Thanks for the tips.

  47. Miguel says:

    coming from a *nix background (and happy to have added mac to my world) I added this to my crontab

    0 8 * * * * cd ~/Library/Mail && sqlite3 -line Envelope\ Index ‘vacuum subjects;’ > /dev/null

    but… (there’s always a but…) I googled for sqlite3/mail.app interaction and I cannot find any resources to tell me if running vaccum while having mail.app open is a bad thing (I don’t close mail.app every night)

    any thoughts ?

  48. Ian L. says:

    You could put a bit of applescript to your cron job to quit Mail before you mess with the Envelopes database:

    osascript -e ‘tell application “Mail” to quit’

  49. Steven B. says:

    This process worked great it not only speeds up Maill.app it also relinked e-mail that was missing from my mailbox.

    Thanks for the tip.

  50. Agnes says:

    I had phantom e-mails in my inbox, pulled the library/mail folder onto the desktop and restarted Mail. A bunch of e-mails I had already received came streaming in as new. So the phantoms are gone, but how do I reimport all of the e-mails that were there? I put the Mail folder back in the library folder in the finder, restarted the computer. When I try to use “Import Mailboxes” in Mail, I choose “Import data from other” and when I direct it to the messages (which have an “emix” extension) I get an error message that “No valid mbox files were found.”

Leave a Reply