0
Delete a task
One script reply has been approved by the moderators Verified

A specific, existing task can be deleted by making a DELETE request on the URL for that task. Deleted tasks go into the “trash” of the user making the delete request. Tasks can be recovered from the trash within a period of 30 days; afterward they are completely removed from the system.

Returns an empty data record.

Created by hugo697 387 days ago Viewed 8978 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 387 days ago
1
type Asana = {
2
  token: string;
3
};
4
/**
5
 * Delete a task
6
 * A specific, existing task can be deleted by making a DELETE request on
7
the URL for that task. Deleted tasks go into the “trash” of the user
8
making the delete request. Tasks can be recovered from the trash within a
9
period of 30 days; afterward they are completely removed from the system.
10

11
Returns an empty data record.
12
 */
13
export async function main(
14
  auth: Asana,
15
  task_gid: string,
16
  opt_pretty: string | undefined,
17
  opt_fields: string | undefined
18
) {
19
  const url = new URL(`https://app.asana.com/api/1.0/tasks/${task_gid}`);
20
  for (const [k, v] of [
21
    ["opt_pretty", opt_pretty],
22
    ["opt_fields", opt_fields],
23
  ]) {
24
    if (v !== undefined && v !== "") {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "DELETE",
30
    headers: {
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: undefined,
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41