Script to safely eject Photos drive?

I’ve been having a conversation with ChatGPT about a script to safely eject an external SSD with my Photos library. I have a handful of drives connected to a CalDigit Thunderbolt 3+, and I have a shortcut that ejects all drives, but I always get the “in use” error for the Photos drive, regardless of what apps are open. Error messages after running a script that closes Photos, kills background processes that can block eject, then eject the SSD, say that

macOS’s background processes (mediaanalysisd and managedclientd) are actively holding open the Photos Library, specifically for:

  • Media analysis tasks (face recognition, object detection, memories, etc.)
  • Spotlight indexing, even though it’s supposed to be idle

The updated script looks like this:


VOLUME_NAME="Crucial X6"
VOLUME_PATH="/Volumes/$VOLUME_NAME"

echo "⏏️  Preparing to safely eject '$VOLUME_NAME'..."

# Quit Photos if running
osascript -e 'quit app "Photos"'
sleep 2

# Kill known blockers
echo "🔪 Killing media background processes..."
sudo killall photolibraryd 2>/dev/null
sudo killall cloudphotosd 2>/dev/null
sudo killall mediaanalysisd 2>/dev/null
sudo killall managedclient 2>/dev/null
sudo killall Spotlight 2>/dev/null
sudo killall mds 2>/dev/null
sleep 2

# Try to unmount
echo "📤 Attempting eject..."
diskutil unmountDisk "$VOLUME_PATH"

if [ $? -eq 0 ]; then
    echo "✅ Successfully ejected '$VOLUME_NAME'."
else
    echo "❌ Still in use. Processes still holding it:"
    lsof | grep "$VOLUME_PATH"
fi

But I’m womdering if there is some interference or possible problem because of iCloud Photos. I don’t want to start diving too deep into the terminal stuff if iCloud is involved. I wouldn’t think so, but I’m kind of out over my skis here.

mediaanalysisd and photolibraryd, at least, will immediately restart after being killed.

1 Like

I gave up on this and got my external photo library into a Satechi hub that is under my Mac, always connected.

Killing the processes I believe to be safe, but force disconnecting the drive will almost certainly trigger a “Photo library is corrupted” issue. The issue with this script is that potentially the unmountDisk procedure can fail if some of the daemons processes are restarted by launchd.

A more complicated approach would be telling launchd to stop the processes, eject the drive, and then tell launchd to launch them again.

It’s not just photo library processes that will scupper an eject. I get the “in use” error from time to time on drives that only have plain old files on them that I put there and move elsewhere. I complete the putting/moving and go to eject the drive and it says it’s busy doing something.

I swear some 5+ years ago Apple promised to improve this situation. They didn’t. Usually in these cases, I know what I was doing with the data, so I just force it.

Another annoying one is “this drive is shared”. Yes, but no-one is currently sharing it. No matter. It’s shared! :angry:

I used to have this issue with my TM drive, something to do with Spotlight. However, once I put that drive on the “Privacy” list, that problem went away. I have since put this drive on the privacy list, to no avail. Long troubleshooting sessions with ChatGPT keep coming up empty, outside of force eject or disconnecting while logged out. I’ve read other comments attributing this issue to the Crucial drive itself, as in, this wouldn’t be a problem if not using this Crucial X6 drive.

This isn’t enough of an inconvenience to get me to go buy a differently-branded 1TB SSD though. I guess I’ll just be logging out now when I want to go portable.

EDIT:
I just remembered that I have another Crucial X6 attached to the Caldigit that does not give me grief when I run the “Eject All” shortcut. So the brand isn’t necessarily the issue. * sigh *