Recover deleted messages in Mail.app
Mar 10, 05:13 PM by Eric Allen
I accidentally selected all of the messages in my archive folder in Mail.app today and hit delete. I was trying to delete everything in my inbox, but missed. I’ve got it set up to not use a trash, so the messages just disappeared. However, upon further inspection they were still sitting on my hard drive. One hint I found while Googling gave me an idea. If the messages are still there (instead of in a backup as in that case), all I have to do is delete the Envelope Index file and they should all come back! Sure enough, after navigating to ~/Library/Mail and deleting the 13MB Envelope Index file, Mail started up and decided it needed to index all of my mail again. However, I ended up in the same predicament, with all of my messages indexed but not visible. So, with a little more digging I discovered that each emlx file has some flags embedded in it, including one for deletion. No problem, I just whipped up a Ruby script to go through an emlx file, search for the flags, and undelete it. It took about five minutes to go though all of my mail, but it worked.
Procedure
- Close Mail.app
- Make a backup of ~/Library/Mail (just in case)
- Delete ~/Library/Mail/Envelope Index
- download my ruby script
- navigate to the Messages directory of the mail folder you want to recover in Terminal (e.g. ~/Library/Mail/Mailboxes/Archive.mbox/Messages)
- run the following commands in terminal
mkdir tempfor i in `ls *.emlx`; do ruby [path to ruby script] < $i > temp/$i;donerm *.emlxmv temp/* .
- open Mail.app
- Go to the Mailbox menu and select “Rebuild” at the bottom
- see your mail magically appear!
Email me if you need any help with this since I have failed to make it robust or include the actual shell commands.
Update: I just did this to my sent folder again and the process worked beautifully.