How to Delete ALL users from a macOS system to prepare it for sale
Hello. The "remove .AppleSetupDone" trick has been around for years to trigger the Setup Assistant to start again on the next boot. I've been using this to allow me to setup a used Mac for sale that has all the latest macOS updates as well as some additional basic free apps installed like Malwarebytes, VLC, OpenOffice, etc. that I think give it some additional value for my buyers. Sonoma breaks this I understand but that's not my concern. My concern is, when I setup a user, that "account" seems to still exist. I know this because if I try to setup the mac again after "resetting" it, I cannot reuse the previous username. For example, if the mac was setup with a user named "user", after deleting .AppleSetupDone and rerunning setup, if I try to use "user" again it automatically changes it to "user1". It will not let me use "user". If you look at the steps I use to accomplish this you will probably know why - none of these steps actually removes the "account". It removes the directory, yes. But the account still exists somewhere. Here is what I've been doing:
Big Sur/Monterey (From Recovery Terminal, not Single User)
csrutil disable
rm /Volumes/Macintosh\ HD/var/db/.AppleSetupDone
rm /Volumes/Macintosh\ HD/Library/Keychains/apsd.keychain
rm -rf /Volumes/Macintosh\ HD/var/db/ConfigurationProfiles/
rm -rf /Volumes/Macintosh\ HD/Users/user
csrutil enable
Older versions of these commands also had
sudo /usr/bin/dscl . -delete "/Users/<username>"
but I could never get that command to do anything but produce an error. I see a version of that command mentioned in other sources when I google "how to remove a user account through terminal in macOS". But none of those links seem to be applicable to a system booted from recovery mode.
Apparently there is a new command
sysadminctl -deleteUser $username
but again, that seems to only work when booted normally and signed in to an admin account. I hope I have explained this well and someone can shed some light on how to accomplish what I need.
Thanks in advance,
Larry