How to delete user profiles

checkmate1984
New Contributor

Hi all

We have mac suite with 31 devices. Ech device has like 150gb or so used up by students profiles. 1gb -5gb per user.

What is best way to delete these? can it be done via jamf pro? If it can delete on log off or after days it be better to avoid doing manual work

 

thanks

2 REPLIES 2

AJPinto
Honored Contributor II

If you are not already, I would recommend reimaging the Macs during summer and winter breaks. Either way, you can do this with a script which Jamf can deploy. You can make the script as fancy as you want. Keep in mind, automating data deletion can be very risky.

 

An example I tossed together; I have not tested this at all and likely wont run as is. Just an idea for a workflow.

#!/bin/bash

# Get the current date in seconds since epoch
current_date=$(date +%s)

# Iterate over all user accounts
while IFS=: read -r username _ _ _ _ home_dir _; do
    # Skip the "Admin" account
    if [ "$username" == "Admin" ]; then
        continue
    fi

    # Skip system accounts and non-existent home directories
    if [[ "$home_dir" == "/var" || ! -d "$home_dir" ]]; then
        continue
    fi

    # Get the last login date for the user in seconds since epoch
    last_login_date=$(dscl . -read "/Users/$username" | grep -A 1 "LastLogin" | tail -n 1 | awk '{print $2}')

    # Check if the user has not logged in within the last 30 days
    if [[ -n "$last_login_date" && $((current_date - last_login_date)) -gt $((30 * 24 * 3600)) ]]; then
        echo "Deleting user: $username"
        
        # Delete the user account
        sudo dscl . -delete "/Users/$username"
        
        # Delete the user's home directory
        sudo rm -rf "$home_dir"
    fi
done < <(dscl . -list /Users UniqueID | awk '$2 >= 501 { print $1 }')

echo "User cleanup complete."

 

 

Qwheel
Contributor II

I don't think programatically deleting user accounts will work in most environments.
For example, we have a pleathora of run once at login scripts that won't trigger again if a user is simply removed - they have to be manually flushed via jamfcloud... furthermore - there isn't an JAMF classic API call that can be used to flush policies at the same time.