10.5: Delete users accounts from command line in 10.5

Jun 09, '09 07:30:00AM

Contributed by: nilness

I work at an Apple authorized service provider, and many times we need to do a new OS X install, create a (temporary) user account, run all the updates, then delete the user account and get the machine back to "new" condition, i.e. no user accounts and Setup Assistant runs at boot.

The process in 10.4 is pretty well documented, but not so 10.5. Here's a process I've come up with and tested twice. It's not as elegant as just deleting the whole netinfo database, but it still seems to accomplish what I need. Hopefully someone more knowledgeable can improve it!

To delete a user from the command line in 10.5:

  1. Boot into single user mode. Hold Command-s at startup.
  2. Check the filesystem: /sbin/fsck -fy
  3. If no remaining errors, mount the filesystem: /sbin/mount -uw /
  4. Start up directory services:
    launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
    Note that single user mode said to use com.apple.DirectoryServicesLocal.plist, but that didn't work -- this did.
  5. Find what groups the user belongs to: dscl . -list /groups GroupMembership | grep username -- repeat for each group except for the user's own group.
  6. Remove the group corresponding to the username: dscl . delete /groups/username (this may not be necessary -- you may get an error that the group doesn't exist; you can ignore it and go on).
  7. Remove the user account: dscl . delete /users/username
  8. At this point, you may wish to remove or archive the user folder in /Users.
  9. You may wish to remove the .AppleSetupDone file in /var/db to cause the Setup Assistant to run when next booted.
  10. All done? Type reboot to reboot the system or shutdown -h now to shut down the system.
Much of this was taken from this entry in Robert Daeley's blog at O'Reilly. Please know what you're doing before trying this -- the dscl command will let you wreak havoc on your system if you're not careful!

Comments (18)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20071030151739791