0

Delete File or Folder

by
Published today

Move a file or folder to the recycle bin. `item` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). Deleting a folder deletes everything in it. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.

Script ms_onedrive Verified

The script

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

3
const graph = "https://graph.microsoft.com/v1.0"
4

5
function driveUrl(drive_id: string | undefined) {
6
  return drive_id != null && drive_id !== ""
7
    ? `${graph}/drives/${encodeURIComponent(drive_id)}`
8
    : `${graph}/me/drive`
9
}
10

11
// An item is addressed by its id, or by a path from the drive root starting with "/"
12
function ref(item: string | undefined) {
13
  if (item == null || item === "" || item === "/") return "root"
14
  if (!item.startsWith("/")) return `items/${encodeURIComponent(item)}`
15
  const path = item
16
    .replace(/\/+$/, "")
17
    .split("/")
18
    .map(encodeURIComponent)
19
    .join("/")
20
  return `root:${path}:`
21
}
22

23
/**
24
 * Delete File or Folder
25
 * Move a file or folder to the recycle bin. `item` is an id, or a path from the drive root starting with "/" (e.g. `/Reports/2026/summary.xlsx`). Deleting a folder deletes everything in it. Requires Files.ReadWrite (a connection default); a drive that is not your own needs Files.ReadWrite.All.
26
 */
27
export async function main(
28
  auth: RT.MsOnedrive,
29

30
  item: string,
31
  drive_id: string | undefined
32
) {
33
  const drive = driveUrl(drive_id)
34
  const response = await fetch(`${drive}/${ref(item)}`, {
35
    method: "DELETE",
36
    headers: { Authorization: `Bearer ${auth.token}` },
37
  })
38

39
  if (!response.ok) {
40
    throw new Error(`${response.status} ${await response.text()}`)
41
  }
42

43
  // 204 No Content
44
  return { success: true }
45
}
46