09.10.08
Disabling Spotlight in OS X Leopard for the removable drives
I have finally got tired with OS X Leopard's Spotlight. It is a great tool to quickly find the documents and programs but it is so annoying when it comes to the external disks! I have a 500Gb USB disk that I use for backups. USB is not so fast but once I connect it, Spotlight starts indexing it, which, obviously, does not make the things faster. In addition to that, it takes a lot of extra space for nothing.
Existing solutions include either disabling Spotlight entirely (I still want it to be active for my primary disk), pretending that the problem does not exist (Apple way
) or buying a commercial tool that disables the Spotlight for a particular drive. Adding the drive to the "Privacy list" does not help, once I disconnect the external drive, it disappears from the list.
I have declared a war against Spotlight. Basically, all I needed was a hook that I could use to run a script once a drive is connected. I would run "mdutil" to disable indexing and that would be all I need.
Apple provides a service called "launchd" that allows to launch something on an event. And, luckily, there is a flag called "StartOnMount" that allows to schedule a command to be executed when a filesystem is mounted.
The rest was easy. Here is an example of the configuration file for launchd that does the trick:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC
"-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.myhowto.stop.spotlight</string>
<key>Program</key>
<string>/usr/local/bin/stop-spot.sh</string>
<key>StartOnMount</key>
<true/>
<key>RunAtLoad</key>
<false/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
As result, once any filesystem is loaded, the script "/usr/local/bin/stop-spot.sh" will be executed. And the script may look like this:
#!/bin/sh
function shut_spotlight_for_volume() {
if [ -d "$1" ] ; then
mdutil -E -i off "$1"
fi
}
shut_spotlight_for_volume "/Volumes/NO NAME 1"
shut_spotlight_for_volume "/Volumes/MAC_BACKUP"
shut_spotlight_for_volume "/Volumes/MY_VOLUME"
The contents of the script may vary - you may erase the .Spotlight directly of the volume, kill "mds" server etc - whatever you want to do to stop this process from doing the job you do not want to be done ![]()
The last step is to load this configuration file into launchd. You can do it by using the command like this (assuming that the XML configuration file is saved to /System/Library/LaunchDaemons/org.myhowto.stop.spotlight.plist) :
sudo launchctl load /System/Library/LaunchDaemons/org.myhowto.stop.spotlight.plist
For more information on the commands involved, read the manual pages for "launchd", "launchd.plist", "launchctl" and "mdutil"
Dick Guertin said,
November 18, 2008 at 4:13 pm
I found another way that works for any external disk volume on both Leopard and Tiger, and once done, you don’t have to do it again. If you’re interested, send me and email and I’ll send you my “spotstop” shell script, along with instructions.
Dickster
DBL said,
December 20, 2008 at 5:53 pm
Either you were too quick to judge the situation, or something is wrong with your system. In Leopard, when I add a drive to the Privacy tab in the Spotlight System Preferences Pane, yes it disappears when I eject the drive, but it reappears again when I reconnect the drive — I don’t have to re-add it to the privacy pane: it remembers. Just because it is not on the list when it is not connected, doesn’t mean it forgot! Just plug it back in again and it will remember not to index it. Try it yourself! Like I said, either your system is not behaving in the normal way, or you just leaped to a conclusion because it was not on the list and this caused you to do a bunch of extra work and write a ranty blog post for nothing.
Nick said,
January 3, 2009 at 10:40 am
DBL,
Probably it works for some devices, but it definitely does not work for my USB card reader and USB external hard drive. May be the keyword is actually “USB”, Apple does not like it
Anyway, after re-connecting the same card reader, even with the same memory card, the path does not re-appear in the Spotlight preferences and the Spotlight DOES start indexing it.
BTW, if you do a quick search on Google you will see that a lot of people are having the same issue.
Please do not start with “Firewire vs USB”
mozami said,
April 8, 2009 at 6:35 pm
I agree with Nick - it begins indexing the drive every time you re-plug it into the USB port, regardless of weather or not it was listed in the “do-not-index” list.
I came across another solution that seems to be working thus far and you can give it a shot too: just add an empty file called .metadata_never_index to the root of your drive and this should eliminate your indexing issues without resorting to any additional scripts.
You can create the file by simply going to the usb drive in the terminal and typing touch .metadata_never_index
Jason Young said,
August 9, 2009 at 10:51 am
I agree with DBL. I plugged in my imation 1gb usb, went to system pref> spotlight> privacy and chose my usb. i unplugged it, then checked privacy and the volume disappeared. I plugged it again, checked privacy, and the volume reappeared. thanks!
wrigley said,
November 4, 2009 at 6:59 pm
If the only harddisk you want indexed is your main/internal harddisk, and that is called “Macintosh HD” you could disable indexing for all other harddisks replacing the last 3 lines in your script with
for i in /Volumes/*;
do if [ "$i" != "/Volumes/Macintosh HD" ];
then shut_spotlight_for_volume “$i”;
fi;done;
That way you don’t have to add the name of every friends usb pendrive when they quickly need something, etc.
/wrigley
Dick Guertin said,
January 10, 2010 at 8:35 pm
I have an external disk drive partitioned into multiple volumes. Some are for backup use on Tiger, and some are for Leopard. Therefore, this drive is mounted to BOTH Tiger and Leopard systems, but “mdutil” on Tiger is expecting to find .Spotlight-V100/_IndexPolicy.plist, and Leopard is expecting .Spotlight-V100/Store-V1/VolumeConfig.plist, so you need to disable using each mdutil that comes with each system. Alternatively, as mentioned by “mozami”, you can:
cd /Volumes/your\ target\ volume
sudo touch .metadata_never_index
The existence of his file seems to be recognized by Spotlight on both Tiger and Leopard. But it doesn’t stop indexing in progress, and you can’t unmount while indexing is happening. So you need to stop indexing first with mdutil, and then add the .metadata_never_index file. At least then you can unmount the volumes (entire disk drive), and safely mount them of EITHER Tiger or Leopard. Also, you need to be concerned about your backup software. If you are making a “clone”, then .Spotlight-V100 on your boot volume will be copied to your backup, and you’ll undo the “disable Spotlight” work you did to prevent your backup from being indexed. Some backup software allows you to exclude certain directories (like .Spotlight-V100), yet retains what’s already on backup.
I’ve written a wrapper script that creates .metadata_never_index on my boot volume, does the clone backup, and then removes that file from my boot volume. That way, the backup software always copies .metadata_never_index to the backup volume, and prevents indexing the next time the backup volume is mounted.
gaetano said,
March 3, 2010 at 8:02 am
at wringley, instead of
for i in /Volumes/*;
do if [ "$i" != "/Volumes/Macintosh HD" ];
then shut_spotlight_for_volume “$i”;
fi;done;
I believe the following is safer
find /Volumes -user $USERNAME -maxdepth 1 -exec mdutil -E -i off ‘{}’ \;
where USERNAME= your user name
chris said,
September 3, 2010 at 11:26 am
Now i need one of those things that will FORCE spotlight to index my USB drive!!
since your no.1 in the search for this - i thought i’d ask you
admin said,
September 3, 2010 at 1:34 pm
I believe running something like “mdutil -E -v” should help. Or at least explain why it does not want to index it by default.
peri said,
November 1, 2010 at 4:14 pm
Please send me your “spotstop” shell script and instructions. I have two external clones of my startup drive, so spotlight searches give results in triplicate. I fixed the same problem for the “Open With” dialog using: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user. I’d like something simple like that for spotlight, I suspect your “spotstop” is that.