31

Is there any way to programmatically add a folder to Spotlight's "Privacy" tab? Either through Terminal or Applescript?

Often I want to click on an item in Spotlight results in Finder, and have that item's folder no longer be indexed.

I found that if you

sudo defaults read /Volumes/foo/.Spotlight-V100/VolumeConfiguration.plist Exclusions

you can see an array with all the items excluded.

But if you try to

sudo defaults write  /Volumes/foo/.Spotlight-V100/VolumeConfiguration.plist Exclusions -array-add '/path/to/folder' 

it does NOT get added to the Privacy tab in the Spotlight Preference Panel, nor does it get de-indexed.

bmike
  • 235,889
d0g
  • 3,902

5 Answers5

26

According to an answer here, a folder can be excluded from Spotlight indexing (achieving the same effect as adding it to the Privacy tab, although the folder won't actually be listed there) by either

  • adding .noindex to the end of the folder's name, or
  • creating an empty file .metadata_never_index inside the folder (eg with touch folder/.metadata_never_index)

You will need to restart Finder after adding .metadata_never_index, either killall Finder from Terminal or logout from the Apple menu and then log back in. This method doesn't work in Mojave and later.

anki
  • 11,753
Ashley
  • 4,187
  • 1
    @ankii You've added "This method doesn't work in Mojave and later". Does that apply to the entire answer, or just the final paragraph? – Ashley Nov 18 '19 at 13:55
  • Just the .meta_never_index method. See some discussion here: https://apple.stackexchange.com/questions/375396/spotlight-ignores-metadata-never-index?noredirect=1&lq=1 I am doubtful of High Sierra, but Mojave is surely ignorant of the same. If you want to edit it, put a space before You will need.. and it will fix the ambiguity of which method it applies to. – anki Nov 18 '19 at 17:04
  • Thanks. Hopefully these comments document it well enough. – Ashley Nov 18 '19 at 23:20
  • If the .metadata_never_index doesn't work on Mojave, then how can we disable indexing on a whole volume? (not just a single dir) – Óscar López Jul 31 '20 at 18:27
  • @ÓscarLópez I think that's worthy of an entirely new question - this question is about folders, and my guess is that changing this at the volume level would need quite different solutions. – Ashley Jul 31 '20 at 19:54
  • @Ashley I asked here because for a mounted volume, it used to be the case that just creating the .metadata_never_index at the root was enough, it behaves like a folder. I don’t know what to do now :-/ – Óscar López Jul 31 '20 at 20:29
  • @ÓscarLópez Sorry for delay - I forgot about this. I'd suggest asking a new question, with details of your system, what you've tried, what you are seeing and what you expected instead. Then put another comment here with a link to it, and I'll try and help (although can't promise anything). – Ashley Aug 06 '20 at 15:30
  • Or see Cameron Lowell Palmer's new answer – huyz Sep 25 '23 at 05:12
11

The VolumeConfiguration.plist has moved

It now resides in /System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist.

You can easily edit it using /uar/libexec/PlistBuddy

Reading

sudo /usr/libexec/PlistBuddy -c "Print :Exclusions" /System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist

Writing

The zero indicates the element in the array or in this case the beginning of the array

sudo /usr/libexec/PlistBuddy -c "Add :Exclusions:0 string '/path/to/folder'" /System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist

Deleting

The zero indicates the the first item in the array

sudo /usr/libexec/PlistBuddy -c "Delete :Exclusions:0 string" /System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist

Restart the daemon

Easiest solution to get the change to take effect is reboot, but you can also tell Launchd to restart the mdworkers

sudo launchctl stop com.apple.metadata.mds
sudo launchctl start com.apple.metadata.mds

mdutil

If however, you decide you want to disable spotlight indexing on the whole volume, for example if the machine is a headless build machine, you could just use mdutil.

mdutil -i off /
  • 1
    While the commands you posted do as stated, they do not change the current state of Spotlight indexing, nor does the added entries show in System Preferences > Spotlight > Privacy. So unless you have a way to also make the changes go into effect, then not sure of the value of this answer. – user3439894 Sep 28 '21 at 22:06
  • You'll need to restart of course or restart the daemon – Cameron Lowell Palmer Jan 10 '22 at 10:22
  • 1
    If you have an error running these commands (e.g., Error Reading File: ...), you probably need to give your terminal program full-disk access (under System Preferences > Security & Privacy > Full Disk Access). – robenkleene Jun 12 '22 at 19:04
  • @user3439894 if you want your changes to appear under Spotlight > Privacy, you will need to do so manually. Maybe there is an MDM way? However, the goal here is to disable searching for a given directory, so in that I believe this works. – Cameron Lowell Palmer Jun 26 '22 at 09:49
6

You have to stop and start the launchd service called com.apple.metadata.mds. So after:

sudo defaults write /Volumes/foo/.Spotlight-V100/VolumeConfiguration.plist Exclusions -array-add '/path/to/folder'

do:

sudo launchctl stop com.apple.metadata.mds && sudo launchctl start com.apple.metadata.mds
casr
  • 161
0

First you have to change the permission settings to add something to the spotlight privacy tab sudo chown -R $USER:$GROUP /.Spotlight-V100/ sudo chmod -R 777 /.Spotlight-V100/

and then

sudo defaults write /.Spotlight-V100/Store-V1/Exclusions Exclusions -array-add should work.

And don't forget to set it back afterwards.

myhd
  • 3,480
  • 1
    When I did -array-add the folder was added to the plist; it just didn't have any effect. In other words, it showed up with defaults read, but it did NOT show up in the Pref Panel. Do you think this permission mod would make a difference? – d0g Mar 29 '13 at 07:03
  • I am not so sure, but the defaults are -rwx-------? – bluewoodtree Mar 29 '13 at 17:11
  • Sorry I misunderstood, I thought it would not get added at all. I am not so sure, but the defaults are -rwx-------? In this case you may be right and it should not make any difference if you have it changed -rwxrwxrwx with chmod 777. But it is worth a try!? – bluewoodtree Mar 29 '13 at 17:18
  • I believe Store-V1 is only used by older versions of Mac OS X... it is empty on my systems with 10.9 Mavericks and later. You might try modifying the command to use Store-V2 instead. – Jim Grisham Nov 13 '17 at 04:51
  • For me it just says operation not permitted. I'm on mojave. – Jonathan Jun 08 '20 at 14:00
-5

You could also just go to the system prefs pane for Spotlight. There you'll find a privacy tab. Just add the folders and files you don't want to show up in the index. That's it. They've already made it easy so you don't have to use the command line.

And If it's added to the privacy tab you can see what all you have hidden from the index just in case you want to index it it later.

  • 1
    The question was about how to do this programmatically. Obviously it can be done via GUI. – d0g Apr 19 '13 at 17:12
  • 1
    @Ze'ev: A valid point... but next time, since doing it programmatically is so central to the question, you should probably include "programmatically" in the title. – LarsH Jan 23 '15 at 16:35
  • @Ze'ev: your question details disagree with your title. You need to make your title clear and accurate to the question – smci Jul 31 '15 at 17:05