Apple - Prevent Spotlight from indexing external drive

You could have a script that runs at startup that employs the technique suggested in this post

When booting from DriveA (when you want to disable spotlight indexing for External DriveB) you could execute :

touch /Volumes/DriveB/.metadata_never_index

When booting from external DriveB and you want to re-enable spotlight perhaps you could have your startup script execute:

rm /Volumes/DriveB/.metadata_never_index

The linked post also lists other ways to programatically alter the spotlight exclusions.

Here are some ways to add a script that will launch at login :

Good luck!

Edit : Method using bash scripts and plist files

First create a startup script. I chose to create one at ~/

Make sure it's executable chmod +x ~/

Script for OS that wants to hide a drive from spotlight

flagRemoved=".ney_the_index"  # a new name

# if flag exists rename it.
if [ -a "$flagLocation/.metadata_never_index" ]; then 
    mv "$flagLocation/.metadata_never_index" "$flagLocation/$flagRemoved";

Script on the OS that wants to index the drive


if [ -a "$flagLocation/$flagRemoved" ]; then
    mv "$flagLocation/$flagRemoved" "$flagLocation/.metadata_never_index"

if [ ! -a "$flagLocation/$flagRemoved" ] || [ ! -a "$flagLocation/.metadata_never_index" ] ; then
    touch "$flagLocation/.metadata_never_index"

Create a plist file ~/Library/LaunchAgents/com.user.loginscript.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "">
<plist version="1.0">

Test it by loading and unloading it:

launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist

I learned today that you can use a sudo touch /.metadata_never_index_unless_rootfs in the root directory of each drive to separate Indexes from OS X Boot-Drives. It is a special version of .metadata-never-index, because it will (re)index the drive when you boot from it, but not when you don't.

Apologies for the new answer (not enough rep to comment as I'm new here)

@hapi - I may be confused, but are the scripts the wrong way round?

Script for OS that wants to hide a drive from spotlight: renames .metadata_never_index

Script on the OS that wants to index the drive: creates .metadata_never_index

I thought the presence of .metadata_never_index on the volume meant Spotlight ignored it?