Level Verified

macOS Delete/Disable Users Script

Disable or remove unwanted macOS user accounts with a single script. Leverage Level’s “UsersToDelete” variable for easy batch operations and pair it with script-based monitors or automations to maintain secure, compliant endpoints.

Import into Level

Problem overview

This script streamlines the process of removing or disabling unauthorized, stale, or compromised macOS user accounts by automating tasks that would otherwise require manual attention, ensuring your organization maintains secure and compliant endpoints with minimal effort.

Bash 100s timeout Runs as Local system macOS
#!/bin/bash

# This resource is provided as a convenience for Level users. We cannot 
# guarantee it will work in all environments. Please test before deploying 
# to your production environment. We welcome contributions to our community 
# library

# Define the users to remove
USERS_TO_REMOVE="{{UsersToDelete}}"

# Convert the comma-separated list into an array
IFS=',' read -r -a userArray <<< "$(echo "$USERS_TO_REMOVE" | sed 's/, */,/g')"

for user in "${userArray[@]}"; do
    # Trim spaces from the username
    user=$(echo "$user" | xargs)

    # Check if user exists
    if id "$user" &>/dev/null; then
        echo "Processing user: $user"

        # Check if the user is in the admin group and remove from it
        if dscl . -read /Groups/admin GroupMembership 2>/dev/null | grep -q "\b$user\b"; then
            echo "Removing $user from the admin group..."
            sudo dseditgroup -o edit -d "$user" -t user admin && echo "$user removed from the admin group."
        fi

        # Get user's UUID
        UUID=$(dscl . -read "/Users/$user" GeneratedUID 2>/dev/null | awk '{print $2}')
        
        # Remove UUID from admin group if it exists
        if [[ -n "$UUID" ]]; then
            echo "Removing UUID $UUID from admin group..."
            sudo dscl . -delete /Groups/admin GroupMembers "$UUID" && echo "Removed UUID: $UUID from admin group."
        fi

        # Delete the user account
        echo "Deleting user $user..."
        sudo dscl . -delete "/Users/$user" && echo "Deleted user $user."

    else
        echo "User $user does not exist. Skipping..."
    fi
done

It uses a comma-separated list of usernames from the “UsersToDelete” variable and iterates through each entry, checking for existing accounts before disabling login shells, removing admin privileges, and locking the account. By default, it only disables users, but you can uncomment a line to delete them entirely. The script seamlessly integrates with Level’s root-level permissions, making manual elevation steps unnecessary and ensuring a consistent approach to user account management across all your Mac devices.

Use cases

  • Disabling unauthorized or orphaned macOS user profiles
  • Quickly locking out dormant accounts that pose security risks
  • Streamlining compliance for macOS environments by removing inactive or admin-privileged users
  • Combining with admin compliance workflows for a unified security strategy

Recommendations

  • Configure a script-based monitor in Level to automatically disable or remove users detected as unauthorized
  • If ongoing checks are needed, create a Level automation with a schedule trigger to run regular cleanups
  • Test the script in a non-production environment first to validate the behavior and confirm that password parameters (if needed) are set correctly
  • Uncomment the relevant line to permanently delete users if that aligns with your organization’s security policy
  • Check out the Admin Compliance & Remediation Automation

Frequently asked questions.

Can I delete users outright instead of just disabling them?

Yes. Simply uncomment the “sysadminctl -deleteUser” line and comment out or remove the disabling commands to permanently remove the user.

Does this script require special permissions on the Mac?

No, Level automatically runs scripts as root on macOS, so additional elevation steps aren’t needed.

What if the user has a Secure Token enabled?

The script attempts to disable the secure token before removing privileges, but you must provide a valid password placeholder. Make sure to update this in your final script.

Why does the script say ‘User does not exist or is system-protected’?

This message appears if the username isn’t recognized by the system or if it’s a protected account. In such cases, manual review may be necessary.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.