Skip to content

Instantly share code, notes, and snippets.

@toonetown
Last active September 18, 2017 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toonetown/8507edf1bb1794943f01bc48a5a6b502 to your computer and use it in GitHub Desktop.
Save toonetown/8507edf1bb1794943f01bc48a5a6b502 to your computer and use it in GitHub Desktop.
Permanently deletes the given local users and resets the OS X (Yosemite and later) system to an empty state. After running this script, the setup assistant will run again.
#!/bin/bash
# Permanently deletes the given users and resets the system to an empty state
# Ensure that it runs as root
if [ "$(id -u)" != "0" ]; then
echo "${0} must be run as root, or using sudo"
exit 1
fi
if [ ! "${1}" ]; then
echo "Usage: $0 <User> [User]..."
exit 1
fi
function delete_user {
if [ "${1}" == "none" ]; then return 0; fi
USR="${1}"
GUID="$(dscl . -read /Users/${USR} GeneratedUID | cut -d' ' -f2)"
if [ ! "${GUID}" ]; then
echo "Could not find user ${USR}"
return 1
fi
echo -n "Deleting user ${USR}..."
dscl . -delete /Groups/admin GroupMembers ${GUID} || return $?
dscl . -delete /Groups/admin GroupMembership ${USR} || return $?
dscl . -delete /Users/${USR} || return $?
rm -Rf /Users/${USR} || return $?
echo "Deleted ${USR}"
echo ""
}
for i in $@; do delete_user ${i} || exit $?; done
echo -n "Resetting system..."
rm -Rf /Users/.TemporaryItems &>/dev/null
rm -Rf /Library/Caches/* &>/dev/null
rm -Rf /private/var/folders/* &>/dev/null
rm -Rf /private/var/tmp/* &>/dev/null
rm -Rf /var/vm/swapfile* &>/dev/null
find -x / -name .DS_Store -exec rm -f {} \; &>/dev/null
rm -f /var/db/.AppleSetupDone || exit $?
echo "Finished resetting system"
echo ""
echo -n "Shutting down in 5 seconds <ctrl-C to cancel>..."
sleep 5
shutdown -h now
@toonetown
Copy link
Author

toonetown commented Aug 2, 2016

To run (as root):

curl -sSL https://gist.githubusercontent.com/toonetown/8507edf1bb1794943f01bc48a5a6b502/raw | /bin/bash -s -- ${USER}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment