Controlling iCloud Drive's space usage

Last August, I wrote a post over at Atomic Spin on calling macOS APIs in Automator workflows. My impetus was to control the amount of space iCloud Drive was taking up on my Macs with smaller disks.

I don't know how many people actually read that post, because nobody seemed to notice I didn't actually need to write a Swift script to accomplish the task at hand. Because brctl exists.

(Mind you, I'm glad I wrote the post and did the exploration. I think it's really valuable not just from an Automator perspective, but also simply from a "I want to write a shell script to call macOS APIs" perspective. So I'm not mad. But also… the tool already exists, dangit.)

Anyway, there's this, which I can run on my Desktop, Documents, Downloads, and iCloud Drive folders:

% brctl evict .

When running this, I can see all the files in the given folder transition to "online" mode, where they're not really taking up space on the local disk, and must be opened and downloaded to use.

So what about the opposite? What if I want to download everything because I have more storage than I know what to do with?

Well, this exists:

% brctl download .

But it doesn't seem to work. I think that's because the "download a folder" functionality is just broken, somehow.

But I noticed that when a file is in "online" mode, it's replaced by a hidden file—for example, "README.txt" gets replaced by ".README.txt.icloud". So I could write a script to root through a directory, look for files that match that pattern, and run brctl on each one.

if [ "x$1" == x ]
  echo usage: $0 PATH >&2
  exit 1

find "$1" -name .\*.icloud | grep -v '^\.Trash$' | (
  while read fullpath
    dir=$(dirname "${fullpath}")
    file=$(basename -s .icloud "${fullpath}" | cut -c 2-)
    echo ${fullpath} '=>' ${dir}/${file}
    brctl download "${dir}/${file}"

Here's the gist for that script.

In normal operation, it seems iCloud Drive is pretty decent at respecting my eviction request. New files or changed files can get synced and downloaded, but that's not a huge deal.

My main deal is that iCloud Drive doesn't try to download all… 633.9 GB (bless you, Advanced Data Protection) that I have stored inside it to a Mac that only has a 512 GB drive. There's no need to fill that up.

But on my 4 TB MacBook? Be my guest!

If only it was this easy on my 2 TB iPad Pro… maybe that's a reason to write a Shortcuts-able app.

You'll only receive email when they publish something new.

More from Mattie B
All posts