Deleting Users from Accounts and Users folder

jcshofner
New Contributor III

Two years ago I found a Jamf Nation discussion about deleting user accounts and their home folders in the Users folder. The link to it is https://www.jamf.com/jamf-nation/discussions/4502/remove-old-mobile-accounts.
I used the script from cbrewer, which was posted on Posted: 5/17/2012 at 3:17 PM CDT. I tied this script to a LaunchCtl and placed the plist file in the folder /Library/LaunchDaemons. The script and daemon worked fine until now. The script and daemon are running on MacOS High Sierra 10.13.6. I tested the script and daemon using the MacOS Mojave 10.14.6 verion. I ran a test yesterday and today on both Mac versions, and they are both producing an error. The error message is 'Operation failed with error: eDSOpenNodeFailed.' The script runs great in the terminal outside of the daemon. Has anyone come across this error? What does it mean? What command (dscl or find or other) might have caused the error? The script and plist are listed below...

PList

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <dict> <key>Label</key> <string>com.removeusers</string> <key>Program</key> <string>/Library/Application Support/seu/scripts/rmfolders.sh</string> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <false/> <key>LaunchOnlyOnce</key> <true/> <key>StandardOutPath</key> <string>/tmp/startup.stdout</string> <key>StandardErrorPath</key> <string>/tmp/startup.stderr</string> </dict>
</plist>

Script...

!/bin/bash

userList=dscl . list /Users UniqueID | awk '$2 > 1000 {print $1}'

echo "Deleting account and home directory for the following users..."

for a in $userList ; do find /Users -type d -maxdepth 1 -mindepth 1 -not -name "." | grep "$a" if [[ $? == 0 ]]; then dscl . delete /Users/"$a" #delete the account if [ -d "/Users/$a" ]; then echo "Directory /Users/$a exists." rm -r /Users/"$a" #delete home directory fi fi
done

Would a security update to the operating system cause the error?

2 REPLIES 2

sdagley
Esteemed Contributor II

@jcshofner Unless you are dealing with systems older than macOS 10.13 you should be using sysadminctl to delete accounts: https://osxbytes.wordpress.com/2017/03/15/how-to-remove-accounts-cleanly/

kwoodard
Contributor III

This is what I came up with. Seems to be working properly. Please test and let me know if you see something I missed... I left in the user account names that I keep so you can see where to add in yours...

#!/bin/bash

for home in $(dscl . list /Users | grep -viE '(_|root|micro|basicuser|basicadminuser|jamf)')
do sysadminctl -deleteUser $home
done

exit 0