Rebuilding Spotlight’s Index on OS X (Manually)

After doing a number of disk clean up and optimizations, I found myself in the circumstance of OS X’s spotlight returning no results. Whether I searched for a keyword in Mail, or by Spotlight using Command-Space, I got no results backs – just an empty list for my troubles.

It turns out there’s a neat utility out there called Rebuild Spotlight Index 2.7 that does all the grunt work for you. Problem is, it didn’t work for me.

What’s going on is actually fairly trivial, and it’s possible to simply do everything via the command line.

The metadata utilities need to run as root, so to see what your drive is up to, you’d enter something like: sudo mdutil -s /

This shows the status on the root volume.

To turn indexing on for a volume, you enter: sudo mdutil -i on /

And, to force Spotlight to rebuild its index, you simply erase the master copy of the metadata stores on the volume like this: sudo mdutil -E /

However, while I did all this, Spotlight was still not building the indexed for me.

Here’s how I solved it, using just the Terminal.
First, I wanted to see the schema file, so I printed it out using to the standard input using: sudo mdimport -X

At the bottom of the schema listing, I say a reference to a schemaLocation, and took a shot in the dark that perhaps that Spotlight’s index rebuilding needed to check data against its schema before it would start. To do that, it might need network access, if not back to the local machine.

And, for good measure, I went to check the date/timestamp on the Spotlight directory using: sudo ls -la /.Spotlight*

While most of the files had the timestamp of when I tried to delete the index, not all the files had the current date and time. Additionally, the file sizes were not growing, a good indication the index was not being rebuilt.

Thinking to myself, “what could be causing network traffic, even internally, not to be working”, I realized that I had just rebooted and PeerGuardian2 was currently active and blocking traffic. This is a great tool for blocking malware and unwanted network visitors, but occasionally it gets in the way. So, I turned it off.

Then, I did the following commands to ensure indexing was on, the spotlight metastore was really gone, and that I wanted it rebuilt:
sudo mdutil -i on /
rm -rf /.Spotlight*
sudo mdutil -E /

The moment I did the last command, this time the system sprung to life, the directory /.Spotlight-V100 was created, and the files inside it were growing quickly. Spotlight on the toolbar showed a progress bar, indicating the system would be done indexing in a bit. The big difference? I turned off the network traffic blocker for a moment.

This entry was posted in Apple, How To, Operating Systems, OS X, Software, Workaround. Bookmark the permalink.

13 Responses to Rebuilding Spotlight’s Index on OS X (Manually)

  1. Mugur Baloiu says:

    Thanks for the tip.
    This was very helpful for my external hard drive on OSX Leopard. I had this issue – after the upgrade on Leopard, Spotlight was returning 0 results on a search to the external drive. It was so frustrating because it was perfectly working before Leopard.

    I am glad I could turn indexing on for my volume with this command:
    sudo mdutil -i on /

  2. David Livingstone says:

    Thanks also. Can’t remember BASH syntax, so this helped step through the process with a new Leopard Server X install. It’s now indexing and I can go to bed.

  3. Tim Gibbons says:

    You can force Spotlight to rebuild its database by adding the affected disk to the Spotlight Privacy exclusions in System Preferences, closing System Preferences, then removing it from the exclusion list. Spotlight will begin rebuilding the Spotlight database immediately. This may take a while!

  4. Oliver Lu says:

    Another way to do it semi-automatically is with “Maintenance”, an Automator Apple Script. (It also does other stuff, like repair permissions and clean out caches.)

    It’s free and available through Apple (so presumably it’s safe):

  5. Dan Z says:

    i had spent a while troubleshooting spotlight and finally this worked! changing the options within spotlight preferences had done nothing! i had to use the terminal as above, except my second command also required a preceding sudo as follows:

    sudo mdutil -i on /
    sudo rm -rf /.Spotlight*
    sudo mdutil -E /

    hope this helps others out there with the same problem. my leopard was installed as a generic image by my university as I used the mbpro only for the summer, and i hated how it never worked

  6. Pope says:

    Thank you for this! The TimeMachine on my server went down, and I’ve been freaking out trying to get it to work again. The server has a lot of important stuff on it, and I hated it not being backed up properly.

    You’re a saint.

  7. Monte says:

    I didn’t have anything blocking but, as Dan Z commented, this part of the article did:

    sudo mdutil -i on /
    rm -rf /.Spotlight*
    sudo mdutil -E /

    And that’s after trying lots of other suggestions/apps found on other sites. Thanks much!

  8. MacMedix says:

    I’ve been having spotlight problems. Mail only finds certain items, and the Finder only finds certain items. Really annoying. I’ve also had a problem where sometimes I can’t open the Spotlight pref pane. It tries to open, but gets stuck at “Loading Spotlight”. It does allow the window to be closed. Sometimes it will open though.

    Last night I opened the Spotlight System pref pane, and said NOT to index my boot drive, closed/opened again, and subtracted my boot drive from the exclude list (leaving only the time machine drive). A quick look at the Spotlight menu (upper right of screen) showed that Spotlight was “Estimating time of indexing”. However, 8 hours later is was still claiming to be doing that. I don’t think so.

    sudo mdimport -X
    Says it can not import for me. It’s possible that I may have killed the mdutil process sometime in the past 3 weeks.

    So today I did these 3:

    sudo mdutil -i on /
    rm -rf /.Spotlight*
    sudo mdutil -E /

    And Spotlight again CLAIMED is was preparing to index, just as it had all last night. But it still doesn’t look like it is really working.

    So I rebooted the machine. Yup, I actually rebooted, which I don’t do much. Uptime was 17 days, and would have been far longer but I installed a security update that required a reboot.

    Now the indexing appears to be really working, and the Spotlight window says it will be finished in 48 hours… No wait – 16 hours, uh – 2 hours, ok maybe a total of 4 hours.

    Anyway – a reboot may be what you need to get this thing going as well as the terminal commands. I’m running Leopard OS X 10.5.6 on a 2.16 Ghz intel iMac.

  9. MacMedix says:

    Now Spotlight is doing pretty well in the Finder… But it’s still not finding words in the message body of Mail unless I search “all Mailboxes” . However, it CAN find those same words in those same emails via a Finder Spotlight search.

    So if I type “macworld” (no quotes) in the search bar of Mail, and set it to look in ‘Entire message” and All Mailboxes it finds plenty. Oddly enough, if I tell it to search just one folder/mailbox it finds nothing. If I do the same search for macworld in the same mailbox and Subject, it finds a bunch of messages that have macworld in the subject, and most of those also have macworld in the message body as well. So searching of Mail is still not working properly, and if I really want to find an email, I have to either search “All mailboxes” or leave the Mail program and search from the Finder. Wow, that’s lame.

  10. babua says:

    Thank you so much! After futilely playing around with various utilities, your solution was the one that saved the day. As one other poster said, you /are/ a saint.

  11. Mark says:

    Easy Fix for Spotlight Mail Searches:

    – Highlight the mailbox (do this mutliple times for each mailbox if you have more than one)
    – Go to MAILBOX in the Menu
    – Click REBUILD

    Done. Do the same for the next Mail account you have.

  12. DAR says:

    If you have problems with Spotlight and
    you are using Little Snitch:
    go to Littlre Snitch “Preferences” and press: “New Rule” and configure to “Allow Connections” -“Choose Application”-“System/Library/Spotlight” and then select – one by one – ALL “.mdimporter” files in this folder.

    This will make your Spotlight work across any connected harddrive.

  13. Stefan Kraft says:

    When you upgrade from an older OS you could also check on /Library/Spotlight for old plugins. Some of them are known to eat a lot of performance. I also had problems in OS X Lion with the iWeb plugin. Removing this and the Mircosoft* ones helped me fix my spotlight issues.

Leave a Reply

Your email address will not be published. Required fields are marked *