Empty Trash on macOS
Learn how to remotely empty trash folders on Mac devices using scripts. This helps free up disk space and manage storage efficiently across your Mac fleet.
Empty Trash for Specific User
Remove all items from a specific user's trash:
#!/bin/bash
# Specify the username
USERNAME="john"
# Empty trash for specific user
sudo rm -rf /Users/"${USERNAME}"/.Trash/*
echo "Trash emptied for user: ${USERNAME}"
Empty Trash for Current User
Automatically detect and empty trash for the currently logged-in user:
#!/bin/bash
# Get current user
CURRENT_USER=$(stat -f "%Su" /dev/console)
# Empty trash for current user
su "$CURRENT_USER" -c "rm -rf ~/.Trash/*"
echo "Trash emptied for current user: ${CURRENT_USER}"
Enhanced Script with Validation
Script with error handling and logging:
#!/bin/bash
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S'): $1"
}
# Get current user
CURRENT_USER=$(stat -f "%Su" /dev/console)
if [[ -z "$CURRENT_USER" ]]; then
log_message "Error: Could not determine current user"
exit 1
fi
log_message "Emptying trash for user: ${CURRENT_USER}"
# Check if trash directory exists
TRASH_DIR="/Users/${CURRENT_USER}/.Trash"
if [[ ! -d "$TRASH_DIR" ]]; then
log_message "Trash directory not found: ${TRASH_DIR}"
exit 1
fi
# Count items before deletion
ITEM_COUNT=$(find "$TRASH_DIR" -mindepth 1 -maxdepth 1 | wc -l)
log_message "Found ${ITEM_COUNT} items in trash"
if [[ $ITEM_COUNT -eq 0 ]]; then
log_message "Trash is already empty"
exit 0
fi
# Empty trash
if su "$CURRENT_USER" -c "rm -rf ~/.Trash/*"; then
log_message "Successfully emptied trash for ${CURRENT_USER}"
else
log_message "Failed to empty trash"
exit 1
fi
Multi-User Trash Management
Empty trash for all users on the system:
#!/bin/bash
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S'): $1"
}
log_message "Starting multi-user trash cleanup"
# Find all user directories
for USER_DIR in /Users/*; do
if [[ -d "$USER_DIR" && ! "$USER_DIR" =~ /Users/(Shared|Guest) ]]; then
USERNAME=$(basename "$USER_DIR")
TRASH_DIR="$USER_DIR/.Trash"
if [[ -d "$TRASH_DIR" ]]; then
ITEM_COUNT=$(find "$TRASH_DIR" -mindepth 1 -maxdepth 1 2>/dev/null | wc -l)
if [[ $ITEM_COUNT -gt 0 ]]; then
log_message "Emptying trash for user: ${USERNAME} (${ITEM_COUNT} items)"
sudo rm -rf "$TRASH_DIR"/*
log_message "Completed trash cleanup for: ${USERNAME}"
else
log_message "Trash already empty for user: ${USERNAME}"
fi
fi
fi
done
log_message "Multi-user trash cleanup completed"
Usage with MacFleet
- Choose the appropriate script based on your needs
- For specific users, modify the
USERNAME
variable - Deploy through MacFleet's remote script execution
- Monitor results in the action history
Prerequisites
- Full Disk Access: Grant full disk access to MacFleet agent in System Settings > Privacy & Security > Full Disk Access
- Administrative privileges: Scripts require sudo access for system-wide operations
Security Considerations
- Verify user permissions before running scripts
- Consider backing up important files before mass trash deletion
- Test on limited devices before fleet-wide deployment
Troubleshooting
Permission denied: Ensure MacFleet agent has Full Disk Access User not found: Verify username exists on the target device Script fails: Check if user is currently logged in for user-specific operations
Note: Deleted items cannot be recovered after running these scripts. Always validate on test systems first.