Last active
September 18, 2017 17:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run (as root):