0

Delete folder

by
Published Oct 17, 2025

Deletes a folder, either permanently or by moving it to the trash.

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 folder
7
 * Deletes a folder, either permanently or by moving it to
8
the trash.
9
 */
10
export async function main(
11
  auth: Box,
12
  folder_id: string,
13
  recursive: string | undefined,
14
  if_match: string,
15
) {
16
  const url = new URL(`https://api.box.com/2.0/folders/${folder_id}`);
17
  for (const [k, v] of [["recursive", recursive]]) {
18
    if (v !== undefined && v !== "" && k !== undefined) {
19
      url.searchParams.append(k, v);
20
    }
21
  }
22
  const response = await fetch(url, {
23
    method: "DELETE",
24
    headers: {
25
      "if-match": if_match,
26
      Authorization: "Bearer " + auth.token,
27
    },
28
    body: undefined,
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36