This PowerShell script (delete-users.ps1) enables bulk deletion of Okta users via the Okta API. The script processes users from a CSV file and handles both active and already deprovisioned users appropriately.
- Bulk delete users from a CSV input file
- Support for both production (
okta.com) and preview (oktapreview.com) environments - Automatic user deactivation before deletion for active users
- Direct deletion for already deprovisioned users
- Comprehensive logging with separate CSV files for different outcomes
- 1Password CLI integration for secure API key management
- Interactive API key prompt if not provided
- PowerShell 6.1 or later
- Valid Okta API token with appropriate permissions
- CSV file containing usernames to delete
- (Optional) 1Password CLI for secure API key management
Your API token must have the following permissions:
okta.users.readokta.users.manage
.\delete-users.ps1 -orgurl "your-org" -key "your-api-key" -filepath "users.csv".\delete-users.ps1 -orgurl "your-org" -key "your-api-key" -filepath "users.csv" -preview.\delete-users.ps1 -orgurl "your-org" -key "op://vault/item/field" -filepath "users.csv"| Parameter | Required | Default | Description |
|---|---|---|---|
-orgurl |
No | weather |
Your Okta organization subdomain (e.g., 'mycompany' for mycompany.okta.com) |
-key |
No* | - | Okta API token. If not provided or too short, you'll be prompted to enter it |
-filepath |
Yes | - | Path to CSV file containing users to delete |
-preview |
No | - | Switch to use oktapreview.com instead of okta.com |
*The script will prompt for the API key if not provided or if the provided key is too short.
The input CSV file must contain a header row with a login column:
For each user in the CSV file, the script:
- Retrieves user information from Okta
- Checks user status:
- If DEPROVISIONED: Deletes the user directly
- If ACTIVE: Deactivates the user first, then deletes
- Logs the outcome to appropriate CSV files
The script creates a Logs directory with the following CSV files:
deprov-users-deleted.csv- Already deprovisioned users successfully deletedactive-users-deprovisioned.csv- Active users successfully deactivatedactive-users-deprovisioned-deleted.csv- Active users successfully deactivated and deleted
deprov-users-deletion-failed.csv- Failed to delete already deprovisioned usersactive-users-deprovisioning-failed.csv- Failed to deactivate active usersactive-users-deprovisioned-deletion-failed.csv- Failed to delete after deactivation
deprov-users.csv- Users that were already deprovisionedactive-users.csv- Users that were activenot-found-users.csv- Users not found in the organization
.\delete-users.ps1 -orgurl "mycompany" -key "00abc123..." -filepath ".\users-to-delete.csv".\delete-users.ps1 -orgurl "mycompany" -key "00abc123..." -filepath ".\users-to-delete.csv" -preview.\delete-users.ps1 -orgurl "mycompany" -key "op://Private/Okta-API/credential" -filepath ".\users-to-delete.csv"The script includes comprehensive error handling:
- Invalid or missing API keys trigger interactive prompts
- Network errors are caught and logged
- Users not found in the organization are logged separately
- Failed operations are logged with detailed error information
- Test in Preview First: Always test with the
-previewparameter in your preview environment - Backup Data: Ensure you have backups before running bulk deletions
- Review CSV File: Double-check your CSV file contains only the users you intend to delete
- Monitor Logs: Review all generated log files after execution
- Secure API Keys: Use 1Password CLI or environment variables instead of hardcoding API keys
"Error Occurred While Executing Request"
- Check your API key permissions
- Verify the organization URL is correct
- Ensure network connectivity to Okta
"User Not Found in Org"
- Verify usernames in CSV file are correct
- Check if users exist in the specified organization
- Ensure you're targeting the correct environment (production vs preview)
API Key Issues
- Ensure API key has sufficient permissions
- Check if API key has expired
- Verify the key is correctly formatted
For more detailed instructions, visit: https://support.okta.com/help/Documentation/Knowledge_Article/How-to-Perform-a-Bulk-Delete-of-Okta-Users-With-API