Category filter

Script to empty Trash on Mac

On a Mac, we use the ‘Move to Trash’ option to move files or folders we no longer need into the Trash folder. But the deleted items only accumulate in Trash unless the user chooses to remove them. Generally, as a file is moved to the Trash, its storage space is available only when the Trash is emptied. In order to permanently remove the items from the Trash and release the associated disk space, we need to navigate to the Trash folder and select the ‘Empty Trash’ option. A seamless and quicker way to empty the Trash on macOS devices would be to use scripts. Using the Execute Custom Script action from Hexnode, IT admins can run these customized scripts directly from the UEM console and remotely empty the Trash folder.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Pre-requisite:


Grant full disk access to hexnodeagentd from the Settings app at the endpoint (System Preferences > Security & Privacy > Privacy in Intel Macs and System Settings > Privacy & Security in M1 Macs).

You can remotely grant full disk access to hexnodeagentd by configuring a PPPC profile with All Files access granted to the app.

Empty Trash of a specific user

The rm command permanently deletes all the items from the Trash on a Mac. The username of the local account whose Trash folder has to be emptied needs to be specified in the folder path. To ensure that the script doesn’t fail to delete a specific file or files, add the -rf parameter, which will override issues caused by conflicting permissions.

E.g., To empty the Trash folder of the user ‘John’ on a Mac, use the command:

sudo rm -rf /Users/John/.Trash/*

Empty Trash of the current user

The script below identifies the currently logged-in user on the Mac and empties the Trash of that particular user:

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository