---
title: Vacuuming Mail.app on El Capitan
date: 2015-10-27 09:40
slug: vacuuming-mail-dot-app-on-el-capitan
categories:
- Code
tags:
- macos
- mail
---

[blog]: http://brettterpstra.com/2012/09/15/vacuuming-your-mail-app-index-still-works-wonders/
[mt]: https://twitter.com/mathiastornblom

I'm not using Mail.app on OS X these days (opted for [MailMate](http://freron.com/)), but I received an email from [Mathias Törnblom][mt] updating the database vacuuming trick [I'd posted previously][blog]. It makes the script work for Mail on El Capitan, and maintains backward compatibility. 

The code below will quit Mail, vacuum the SQLite index, then re-open Mail. On a large email database that hasn't been optimized for a while, this can provide significant improvements in responsiveness and speed. To use the script, copy and paste it into a new document in Script Editor (make sure the language is set to AppleScript), and save it as a Script (scpt) file. Then you can run it via a launcher (LaunchBar, Alfred, etc.) or via the Scripts menu.



```applescript
(*
Speed up Mail.app by vacuuming the Envelope Index
Code from: http://www.hawkwings.net/2007/03/03/scripts-to-automate-the-mailapp-envelope-speed-trick/
Originally by "pmbuko" with modifications by Romulo
Updated by Brett Terpstra 2012
Updated by Mathias Törnblom 2015 to support V3 in El Capitan and still keep backwards compability
Updated by @lbutlr for V5 and Container folder in High Sierra and use du
*)

tell application "Mail" to quit
set os_version to do shell script "sw_vers -productVersion"
set mail_version to "V2"
considering numeric strings
	if "10.10" ≤ os_version then set mail_version to "~/Library/Mail/V3/Maildata/Envelope\\ Index"
	if "10.12" < os_version then set mail_version to "~/Library/Mail/V4/Maildata/Envelope\\ Index"
	if "10.13" ≤ os_version then set mail_version to "~/Library/Containers/com.apple.mail/Data/Library/Mail/V5/Maildata/Envelope\\ Index"
end considering

set sizeBefore to do shell script "du -h  " & mail_version & "|  awk {'print $1'}"
do shell script "/usr/bin/sqlite3 " & mail_version & " vacuum"
set sizeAfter to do shell script "du -h " & mail_version & "| awk {'print $1'}"
display dialog ("Mail index before: " & sizeBefore & return & "Mail index after: " & sizeAfter & return & return & "Enjoy the new speed!")
tell application "Mail" to activate
```

[View the code on GitHub](https://gist.github.com/3728277)

---

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff. This content is supported by readers like you. [More about Brett](https://brettterpstra.com/contact.md), including social links.

Copyright © 2026 Brett Terpstra. All rights reserved.
