Category filter

Script to delete user account on Mac

When employees leave an organization, the corporate devices assigned to them are re-distributed to other workers in the firm. Hence, organizations may require to delete the data and user account from the devices. However, manually doing this task is a time-consuming process. IT admins can deploy scripts using Hexnode UEM’s Execute Custom Script remote action to delete user accounts and related data from macOS devices.

Scripting language – Bash

File extension – .sh

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

Delete the user account

The ‘sysadminctl’ has been introduced on macOS 10.13. This command is a utility in macOS that allows administrators to perform various system administration tasks from the command line. It provides a set of commands for managing user accounts and system configurations.

‘sysadminctl -deleteUser’ command will delete the user account from the system. This action is irreversible and will permanently remove the user account and associated home directory.

Notes:

  • The user account should be granted the Secure Token before attempting to delete it using this script.
  • The Hexnode Service app needs to be given full disk access for these scripts to work.
  • The user who has the only Secure Token on the device cannot be deleted. To delete this user, you need to add a new user, grant a Secure Token to it, and then try deleting the former user.


Even after the execution of the script, the user’s home folder will not be deleted. The user data will remain on the device and can be retrieved later if needed by an admin.

Delete directories and files of the user

The ‘rm’ command deletes all files and directories. By utilizing the -r and -f options, you specify whether the directories should be removed recursively and forcefully without prompting for confirmation. The user account and all associated data will be erased from the device if both commands are executed successively.

Notes:

  • Replace <username> with the username of the account to be deleted.
  • You can eliminate spaces in the username when substituting it in the script.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository