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
Empty Trash of a specific user
1 |
sudo rm -rf /Users/’Enter username’/.Trash/* |
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:
1 2 3 4 5 |
# Variable to hold the value of the current user currentuser=$(stat -f "%Su" /dev/console) # Force empty the trash of the currently logged in user su "$currentuser" -c "rm -rf ~/.Trash/*" |