Deleted files sit in the macOS Trash and continue taking up disk space until they're permanently removed. This article walks through building a Maintenance Item that automatically empties Trash items older than a set number of days, across every local user on a device.
Overview
This solution has two parts: a script that finds and deletes old Trash items for every local user account, and a Maintenance Item that deploys that script to your devices on a recurring schedule. For background on Maintenance Items, see Creating a Maintenance Item.
Step 1: Build the Script
Only modify the days variable on line 3 to set how old a file must be before it's deleted:
#!/bin/bash
days="20"
oldIFS="$IFS"
IFS=$'\n'
Users=$(dscl . list /Users | grep -v '_')
for User in $Users
do
Trash="/Users/$User/.Trash"
if [ -e "$Trash" ]; then
Files=$(ls "$Trash") & /dev/null
if [[ $Files == "" ]]; then
echo "no files"
fi
epochToday=$(date +%s)
for f in $Files
do
fileEpoch=$(date -r "$Trash/$f" +%s)
epochDifference=$((epochToday - fileEpoch))
daysSince=$((epochDifference / (60 * 60 * 24)))
if [[ $daysSince -gt $days ]]; then
rm -rf "$Trash/$f"
echo "$Trash/$f"
fi
done
fi
doneThe script loops through every local user account on the device, checks each user's ~/.Trash folder, and permanently deletes any item older than the days threshold.
Step 2: Deploy via a Maintenance Item
Once the script is ready, use it to create a Maintenance Item and deploy it to your devices. Here's what the finished Maintenance Item looks like: