If you’re using Apple’s Mail application on the Mac you’ve probably seen the app slow down over time. One reason why this can happen is because the database used by Mail to store messages can grow to a large size thanks to message fragmenting and gaps in the database when messages are deleted.
Lucky for you there’s a fairly simple solution, made even simpler by using Automator, Apple’s easy-to-use app for automating tasks that often need to be performed repeatedly.
And isn’t that at least part of the reason why people invented computers to begin with–to handle the mundane tasks that repeatedly pop up in our lives?
The road map
Our goal is this: Make Mail faster by cleaning up the mail database.
Because it’s not a good idea to try to do maintenance on an open Mail database, it’s best to quit Mail first, clean things up, then re-open Mail and see if the clean-up resulted in a speed-up.
So, our steps are:
Quit Mail.
Clean up the mail database.
Open Mail.
Automator makes this really easy.
How Automator works
Automator executes a series of actions to achieve a result. You don’t need to know any fancy scripting or programming languages to use Automator. In this example, though, we will use one UNIX command. But we’ll make it easy by simply copying and pasting it when the time comes.
Now, let’s dive in.
Making the workflow
Open Automator (From the Finder’s Go menu, choose Applications and scroll in the window that appears to find Automator.)
In the window that appears, click New Document.
In the sheet that appears, select Workflow and click Choose. Our first step is to quit Mail if it’s running.
In the search field above the list of actions, type quit.
Drag the Quit Application action into the Workflow area.
From the Quit Application pop-up menu, choose Mail. Leave the Ask to save changes box checked. When you’ve finished steps 4, 5, and 6, your workflow window should look like this.Our second step is to issue a command using the shell, an interface to the UNIX system that is running on your Mac.
In the search field above the list of actions, type shell.
Drag the Run Shell Script action into the Workflow area underneath the Quit Application action.
Select all the text in script shell script box and copy and paste the command below depending on which version of the OS is running on your Mac. Be sure to include the semi-colon at the end of the line.
For Snow Leopard (10.6): sqlite3 ~/Library/Mail/Envelope Index vacuum;
For Lion (10.7), Mountain Lion (10.8), Mavericks (10.9), or Yosemite (10.10): sqlite3 ~/Library/Mail/V2/MailData/Envelope\ Index vacuum;
For El Capitan (10.11): sqlite3 ~/Library/Mail/V3/MailData/Envelope\ Index vacuum;
For Sierra (10.12): sqlite3 ~/Library/Mail/V4/MailData/Envelope\ Index vacuum; When you’ve finished Step 9, your workflow should look like this. In our example, we’ve copied and pasted the command for macOS Sierra (version 10.12). The vacuum command cleans the mail database by copying its contents to a temporary file and rebuilds the database into a smaller amount of disk space. This eliminates gaps, defragments the data, and cleans up the database file structure. Our last step is to re-open Mail so we can see if the app feels faster and more responsive. 10. In the search field above the list of actions, type open. 11. Drag the Launch Application action into the Workflow area underneath the Run Shell Script action. 12. From the Launch Application pop-up menu, choose Mail.Congratulations! You’ve made a workflow. And those are our three steps. Now save the workflow. 13. From the File menu, choose Save… and name your new workflow. To run the workflow, click the Run button in the upper right-hand corner of the workflow window. Mail will quit (if it’s running), the database will be cleaned up, and a speedier Mail will be reopened.
But that’s not all
You can run this Automator workflow anytime you want by double-clicking it to launch Automator and clicking the Run button.
But as we said earlier at least part of the reason why people invented computers to begin with was to handle repetitive tasks.
Wouldn’t it be great if the Mac would simply clean up the Mail database every month without we humans having to worry about it?
Well, guess what….
Automating the Mail database clean-up
Let’s have the Mac do all the lifting, both heavy (cleaning up the database) and light (doing it for us).
One of the types of Automator documents that you can choose from is a Calendar Alarm. We could have created this workflow as a Calendar Alarm at the start, but we can also convert the workflow to a Calendar Alarm now.
From the File menu, choose Convert to….
A copy of the workflow will be made in a new Automator window. Select Calendar Alarm and click Choose.
From the File menu, choose Save….
In the sheet that appears, give your calendar event a name and click Save.
Mail will create a new calendar named Automator within the Calendar app on your Mac. The Calendar Alarm will be created as an event on the date and time it is saved, and the workflow will run.
You can open the event and rename it, move it to any other calendar, change the time, and set it up as a repeating event (say the first day of every month), just like any other calendar event.
Go forth and vacuum!
Now that you’ve seen how easy it is to keep Mail running smoothly, fire up Automator and make your experience with Mail just a little bit better. Did this help your Mail workflow? Let me know in the comments section below!
Related
Mike Matthews
Mike Matthews has used Apple products since the days of the Apple II+ and Applesoft Basic. He appreciates the fact that people no longer have to start up apps by loading them from a cassette tape recorder.
Reader Interactions
Comments
Dray Whartonsays
That was incredibly easy… and useful! Thanks, Mike.
Thanks for this great tutorial. I’ve had lots of issues with Mail App ever since I upgraded to Sierra. I’ve followed all of the steps of creating the script and followed the steps to add the Calendar event, but I cannot find the event in Calendar App. There is no Automator “calendar” listed in the column on the left where all of my calendars appear and there is no event on today’s date in the month view of my calendar. Where can I find the Automator calendar? Thanks for your help!
I have used the exact script; but put into a Hazel rule for years, and it has kept my Mail app running well. I wasn’t aware of the version difference and have updated my script to account for the differences between the v2 which I had and the El Capitan, which I use currently. As a note, I didn’t notice any detrimental effects on El Capitan’s Mail when using v2; but always best to have the right one used.
Apple have recently been changing the name of the folder where it stores it’s data for mail. With El Capitan it changed from v2 to v3. So running the old script with the path to the v2 folder was doing nothing at all to El Capitan’s Mail. Similarly if you upgrade to Sierra, Mac OS then puts it’s mail data in a folder called v4 hence the need for a slightly different script for each version.
Since the shell script command changes with each new OS version, there should be an OS version check before running the shell script, as few people will remember to update this thing when they install a new OS version.
I tried creating the Automator workflow as described above on my MacBook Air (mid-2011) running Sierra 10.12.1 and get an error message saying “Run Shell Script failed – 1 error” and referring to the error as “Error: database is locked.” This was from running it within Automator, but Automator won’t even let me save the workflow; when I try, it tells me
The document “XXX.workflow” could not be saved as “XXX.workflow”.
regardless what name I choose in place of XXX.
I was very careful to copy and paste the line for the shell script, including the final semicolon of that line.
I have now tried twice to implement your instructions to the letter, very carefully, and each time there has been a response that there is no such filer words to that effect. I am running OS Sierra, and was careful to use the shell script specific to it which you cited. I should add that I am not very adept on the computer, but I always follow instructions carefully.
Any advice which you can offer would be much appreciated.
Thank you for this – I was able to follow the instructions – very clear! Unfortunately my Mail does not seem to have sped up. Any idea what else may be going on?
thanks!
I also got:
SQL error: near “Index”: syntax error
in OS 10.6.8
– A search revealed this script for 10.6.8:
sqlite3 ~/Library/Mail/Envelope\ Index vacuum
-worked for me
– They also offered this one for Lion to Yosemite:
sqlite3 ~/Library/Mail/V2/MailData/Envelope\ Index vacuum
– which may work for newer OS’s as well
Hello Mike,
When I click “Run” I get the error message” No such File in Directory” ? This for the workflow using Run Shell Script. Typed the line correctly. Using macHigh Sierra version 10.13.1 Can you assist?
I am working on macos high sierra version 10.13.3 (17D47). What will be the script shell script box. I tried using sqlite3 ~/Library/Mail/V4/MailData/Envelope Index vacuum; but when I click on Run it shows the following error:
The action “Run Shell Script” encountered an error: “Error: unable to open database “/Users/manjeetbahety/Library/Mail/V4/MailData/Envelope Index”: unable to open database file”.
That was incredibly easy… and useful! Thanks, Mike.
Thanks for this great tutorial. I’ve had lots of issues with Mail App ever since I upgraded to Sierra. I’ve followed all of the steps of creating the script and followed the steps to add the Calendar event, but I cannot find the event in Calendar App. There is no Automator “calendar” listed in the column on the left where all of my calendars appear and there is no event on today’s date in the month view of my calendar. Where can I find the Automator calendar? Thanks for your help!
I went through the process, but no Automator calendar was created. How would I troubleshoot this step?
Great tip! For the recurring Calendar aspect, does Calendar have to be running? I use BusyCal as my calendar and seldom boot Apple’s Calendar app.
Hey guys
Does this function the same for IMAP accounts?
things seemed to work for me until I saved the Calendar Alarm. A new Calendar did not show up in my Mac OS Calendar Application.
Tried this with Mac OS X Snow Leopard and it returned this error.
SQL error: near “Index”: syntax error
I got the same error. A search offered this script from Mac Observer:
sqlite3 ~/Library/Mail/Envelope\ Index vacuum
worked for me
Also got this error. Searching elsewhere gave a version of the code with double quotes around the path …
/usr/bin/sqlite3 “$HOME/Library/Mail/V4/MailData/Envelope Index” vacuum
This worked for me.
Hi, just trying this out and I get a database is locked error? Any ideas?
I have used the exact script; but put into a Hazel rule for years, and it has kept my Mail app running well. I wasn’t aware of the version difference and have updated my script to account for the differences between the v2 which I had and the El Capitan, which I use currently. As a note, I didn’t notice any detrimental effects on El Capitan’s Mail when using v2; but always best to have the right one used.
Apple have recently been changing the name of the folder where it stores it’s data for mail. With El Capitan it changed from v2 to v3. So running the old script with the path to the v2 folder was doing nothing at all to El Capitan’s Mail. Similarly if you upgrade to Sierra, Mac OS then puts it’s mail data in a folder called v4 hence the need for a slightly different script for each version.
How much impact does this have on modern SSD based computers?
Enjoyed the article and will try this out as an interesting puzzle exercise.
Since the shell script command changes with each new OS version, there should be an OS version check before running the shell script, as few people will remember to update this thing when they install a new OS version.
Thank You – Have never done a script before.
Thank you Mike, it looks like Mail is much snappier than before!
Kind regards,
Yves
Hey Mike,
Great article! Really helpful… but I am getting a message “Error: Unable to open mail database.”
I copied and pasted the script from your article.
What am I missing?
thanks!
I tried creating the Automator workflow as described above on my MacBook Air (mid-2011) running Sierra 10.12.1 and get an error message saying “Run Shell Script failed – 1 error” and referring to the error as “Error: database is locked.” This was from running it within Automator, but Automator won’t even let me save the workflow; when I try, it tells me
The document “XXX.workflow” could not be saved as “XXX.workflow”.
regardless what name I choose in place of XXX.
I was very careful to copy and paste the line for the shell script, including the final semicolon of that line.
Any suggestions?
It works perfectly. Thank you so much!
I get Error: malformed database schema (last_spotlight_check_date) – near “WITHOUT”: syntax error
On Sierra.
Doesn’t work for me — when running the script, it fails, saying the database is locked.
Same with mine – have you found a solution?
Yet on subsequent runs, it does work. Go figure. (Sierra 10.12.1)
My “automator calendar alarms” default to iCloud even though I have an on my mac calendar. Any suggestions how to fix that?
Everything worked as described, except for the last step. Nothing appears in my Calendar.
Running macOS 10.12.2 Beta on MacBook Pro (mid-2010)
I have now tried twice to implement your instructions to the letter, very carefully, and each time there has been a response that there is no such filer words to that effect. I am running OS Sierra, and was careful to use the shell script specific to it which you cited. I should add that I am not very adept on the computer, but I always follow instructions carefully.
Any advice which you can offer would be much appreciated.
Thank you.
Worked like a champ – thanks!
Thank you for this – I was able to follow the instructions – very clear! Unfortunately my Mail does not seem to have sped up. Any idea what else may be going on?
thanks!
Thanx! Great tip!
I also got:
SQL error: near “Index”: syntax error
in OS 10.6.8
– A search revealed this script for 10.6.8:
sqlite3 ~/Library/Mail/Envelope\ Index vacuum
-worked for me
– They also offered this one for Lion to Yosemite:
sqlite3 ~/Library/Mail/V2/MailData/Envelope\ Index vacuum
– which may work for newer OS’s as well
Hope this helps …
Thanks for the tutorial, very useful!
Great input and Mail is cooking with gas….. Thanks Mike for these good, clear, simple instructions that is speeding up my mail program…..
Worked fine! thanks
Hello Mike,
When I click “Run” I get the error message” No such File in Directory” ? This for the workflow using Run Shell Script. Typed the line correctly. Using macHigh Sierra version 10.13.1 Can you assist?
And for High Sierra (10.13), use V5: sqlite3 ~/Library/Mail/V5/MailData/Envelope Index vacuum;
This did not help mail open any faster with El Cap. Do you have any other recommendations-How about rebuilding my mail??
Hi,
I am working on macos high sierra version 10.13.3 (17D47). What will be the script shell script box. I tried using sqlite3 ~/Library/Mail/V4/MailData/Envelope Index vacuum; but when I click on Run it shows the following error:
The action “Run Shell Script” encountered an error: “Error: unable to open database “/Users/manjeetbahety/Library/Mail/V4/MailData/Envelope Index”: unable to open database file”.
Please advise.
Regards
Manjeet Bahety
Hi
I tried the above but get the error message,”unable to open database”
Please help!