Thursday, February 4, 2021 [Tweets] [Favorites]

Deleting DerivedData the Right Way

Jeff Johnson:

Finder is good about automatically unregistering apps with Launch Services when you put them in the trash and empty it (but not before emptying the trash). However, the rm command-line tool bypasses the trash, and thus also bypasses Launch Services. This means that any registered apps in the removed folder remain in the Launch Services database, effectively forever

[…]

I know that a lot of developers like to use a command-line tool to delete DerivedData. So the question is, how do you do this the right way? The answer is, of course, to do it like Finder! You can use AppleScript to control Finder, and you can call AppleScript from Terminal with the osascript command-line tool.

Presumably, -[NSWorkspace recycleURLs:completionHandler:] can also be used to do this programmatically.

Previously:

Update (2021-02-08): Thomas Tempelmann:

NSWorkspace recycleURLs creates a unique thread for each file to be deleted pre-10.15. And if you delete > 8200 items at once, you get a crash in 10.12 and 10.13 due to thread exhaustion.

But I think that only refers to top-level items passed as parameters, so it should be fine for this purpose.

Ali Rantakari’s trash is a command-line tool that uses Apple events to tell Finder to move a file to the trash.

1 Comment

Benjamin Esham

There’s also a command-line utility called “trash” (http://hasseg.org/trash/) which puts files in the trash via the Finder, and which can also empty the trash. It’s available through Homebrew (“brew install trash”) and Nix (“nix-env -iA nixpkgs.darwin.trash”).

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment