DeleteUsers is available as both a script and an application that will delete user account data off of a macOS machine, excluding any pre-defined users.
Managed lab machines have a tendency to run low on space, as users will occasionally leave large amounts of content behind. I put this application together to help streamline the deletion process. Having seen several requests for how to do something like this in the MacAdmins Slack, I decided to make my version of this tool public as sample code. Feel free to tinker with it and modify it to fit your needs.
This application can be manually run at any time to delete user accounts, while the script can be used to automate the process.
The following code is provide as a community resource and as sample code for testing purposes, and is licensed under the terms of Apache license 2.0. No warranty is expressed or implied. Any results from the execution of the following code are 100% the responsiblity of the downloader and executor.
The application can be stored anywhere on the machine, but it is recommended that it be kept in a location only accessible by those with admin access.
The script is intended to run automatically by way of the periodic
utility - placing it in /etc/periodic/daily would be good for a higher traffic lab. (See https://www.alansiu.net/2020/08/26/running-daily-weekly-and-monthly-scripts-in-macos-using-periodic/ for more info.)
VERY IMPORTANT! Make sure you edit these scripts BEFORE first run!
This script will delete ALL user accounts on the machine, with the exceptions of the currently logged in user, and those accounts delcared within the script. By default, the line in the script that declares those accounts is
for home in $(ls /Users | grep -v -e admin -e Shared -e root -e loginwindow)
You may add your admin / testing / other accounts of value to this line by adding additional -e account_name
entries to that line.
Changes can be made within the executable by editing /DeleteUsers.app/Contents/Resources/script
When you launch the application, you will be prompted for admin rights:
From there, the main app window will appear (the "Details" window is closed on first launch, but can be toggled open to see a list of deleted users):
Once the application finds a user to delete, it will prompt you again for permission to admin your computer:
After all users have been deleted, the script will finish, prompting you to quit the application:
The periodic
script will delete user accounts once a drive space usage percentage is reached, with no interaction needed. That amount is defined under the limit
variable.
If the script executes and deletes users, it will send an email through sendmail
on the local macihne. Subject, recipients, and content are defined in the following line:
echo "To: admin@example.com\ncc: other_admin@example.com\nSubject: Disk usage $usep% on $(hostname)\nLocal storage has exceeded $limit%. The following users were automatically deleted: $deletedUsers" | sendmail -f admin@example.com -t admin@example.com other_admin@example.com
This is a very blunt object of a script. Once you give it your admin password, accounts WILL be deleted with no further prompting, and will continue to be deleted until /Users has been scanned completely. Please be sure there is no data that is needed to be saved from those accounts, because it won't be there once you set the script loose.