0

Delete User

by
Published 4 days ago

Delete a user. Deactivates first if still active (two-step delete); permanently removes a deprovisioned user.

Script okta Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
/**
4
 * Delete User
5
 * Delete a user. Okta requires the user to be DEPROVISIONED first: calling this on an active user deactivates it (first call) and a second call permanently deletes it. Set `send_email` to notify admins of the deactivation. This cannot be undone.
6
 */
7
export async function main(
8
  auth: RT.Okta,
9
  user_id: string,
10
  send_email: boolean | undefined
11
) {
12
  const url = new URL(
13
    `${auth.org_url}/api/v1/users/${encodeURIComponent(user_id)}`
14
  )
15
  if (send_email !== undefined)
16
    url.searchParams.append("sendEmail", String(send_email))
17

18
  const response = await fetch(url, {
19
    method: "DELETE",
20
    headers: {
21
      Authorization: `SSWS ${auth.token}`,
22
      Accept: "application/json",
23
    },
24
  })
25

26
  if (!response.ok) {
27
    throw new Error(`${response.status} ${await response.text()}`)
28
  }
29

30
  if (response.status === 204) return { success: true }
31
  return await response.json()
32
}
33