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.

0

5 Answers 5

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.

8
  • 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
    Commented Nov 18, 2019 at 13:55
  • 1
    Just the .meta_never_index method. See some discussion here: apple.stackexchange.com/questions/375396/… 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
    Commented Nov 18, 2019 at 17:04
  • Thanks. Hopefully these comments document it well enough.
    – Ashley
    Commented Nov 18, 2019 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) Commented Jul 31, 2020 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
    Commented Jul 31, 2020 at 19:54
12

Testing Spotlight Privacy

I use mdfind for testing if a method of preventing Spotlight works. I create a folder called spotlight_test place a file that I know Spotlight can find like dSYMs or an image and try searching for it by type:

mdfind "kMDItemContentType == com.apple.xcode.dsym"

or

mdfind "kMDItemContentType == public.jpeg"

You can find other types with mdls

Methods that work

.noindex

Naming a folder with the extension .noindex

mdutil

You can disable spotlight indexing on the whole volume, for example if the machine is a headless build machine, you can use mdutil.

mdutil -i off /

Methods that DO NOT work

VolumeConfiguration.plist

I have spent some time revisiting the /System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist solution that used to work and it would seem that file will not change the indexing behavior, it only reflects the current state of the Exclusions. You can no longer start and stop the mds service without rebooting. Rebooting will display the Exclusion in Spotlight Privacy, but it does not work until you remove it and re-add it. It would seem we need a mach call to one or more services to emulate the behavior of the UI.

PLIST_BUDDY=/usr/libexec/PlistBuddy
SPOTLIGHT_PLIST=/System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist
PRIVATE_FOLDER=${HOME}/hidden_folder

UPDATE_TIME=$(date +"%a %b %d %H:%M:%S %Z %Y")

if [ "$1" == "update" ]; then
    mkdir -p "${PRIVATE_FOLDER}"
    ${PLIST_BUDDY} -c "Add :Exclusions: string '${PRIVATE_FOLDER}'" "${SPOTLIGHT_PLIST}"
    ${PLIST_BUDDY} -c "Set :ConfigurationModificationDate '${UPDATE_TIME}'" "${SPOTLIGHT_PLIST}"
fi

${PLIST_BUDDY} -c "Print :ConfigurationVolumeUUID" "${SPOTLIGHT_PLIST}"
${PLIST_BUDDY} -c "Print :ConfigurationModificationDate" "${SPOTLIGHT_PLIST}"
${PLIST_BUDDY} -c "Print :Exclusions" "${SPOTLIGHT_PLIST}"
4
  • 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. Commented Sep 28, 2021 at 22:06
  • You'll need to restart of course or restart the daemon Commented Jan 10, 2022 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). Commented Jun 12, 2022 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. Commented Jun 26, 2022 at 9: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
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.

5
  • 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
    Commented Mar 29, 2013 at 7:03
  • I am not so sure, but the defaults are -rwx-------? Commented Mar 29, 2013 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!? Commented Mar 29, 2013 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. Commented Nov 13, 2017 at 4:51
  • For me it just says operation not permitted. I'm on mojave.
    – Jonathan
    Commented Jun 8, 2020 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.

3
  • 1
    The question was about how to do this programmatically. Obviously it can be done via GUI.
    – d0g
    Commented Apr 19, 2013 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
    Commented Jan 23, 2015 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
    Commented Jul 31, 2015 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .