0

Delete user

by
Published Oct 17, 2025

Deletes a user. By default this will fail if the user still owns any content. Move their owned content first before proceeding, or use the `force` field to delete the user and their files.

Script box Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Box = {
3
  token: string;
4
};
5
/**
6
 * Delete user
7
 * Deletes a user. By default this will fail if the user
8
still owns any content. Move their owned content first
9
before proceeding, or use the `force` field to delete
10
the user and their files.
11
 */
12
export async function main(
13
  auth: Box,
14
  user_id: string,
15
  notify: string | undefined,
16
  force: string | undefined,
17
) {
18
  const url = new URL(`https://api.box.com/2.0/users/${user_id}`);
19
  for (const [k, v] of [
20
    ["notify", notify],
21
    ["force", force],
22
  ]) {
23
    if (v !== undefined && v !== "" && k !== undefined) {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "DELETE",
29
    headers: {
30
      Authorization: "Bearer " + auth.token,
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40