0

Delete Label

by
Published today

Permanently delete a user label and remove it from every message. Requires gmail.labels or gmail.modify on the connection.

Script gmail Verified

The script

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

3
export type DynSelect_label_id = string
4

5
// Dropdown of the mailbox's user labels (system labels can't be deleted).
6
export async function label_id(auth: RT.Gmail) {
7
  const response = await fetch(
8
    "https://gmail.googleapis.com/gmail/v1/users/me/labels",
9
    {
10
      headers: {
11
        Authorization: `Bearer ${auth.token}`,
12
        Accept: "application/json",
13
      },
14
    }
15
  )
16
  if (!response.ok) {
17
    throw new Error(`${response.status} ${await response.text()}`)
18
  }
19
  const { labels } = (await response.json()) as {
20
    labels: { id: string; name: string; type: string }[]
21
  }
22
  return labels
23
    .filter((l) => l.type === "user")
24
    .map((l) => ({ value: l.id, label: `${l.name} (${l.id})` }))
25
}
26

27
/**
28
 * Delete Label
29
 * Permanently delete a user label and remove it from every message and thread it is applied to. The messages themselves are kept. Requires gmail.labels or gmail.modify on the connection.
30
 */
31
export async function main(auth: RT.Gmail, label_id: DynSelect_label_id) {
32
  const response = await fetch(
33
    `https://gmail.googleapis.com/gmail/v1/users/me/labels/${label_id}`,
34
    {
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
  return { success: true }
44
}
45