1 | |
2 |
|
3 | |
4 | * Delete Product |
5 | * Permanently delete a product and all its variants and media. This can't be undone. |
6 | */ |
7 | export async function main(auth: RT.Shopify, product_id: string) { |
8 | const id = product_id.startsWith("gid://") |
9 | ? product_id |
10 | : `gid://shopify/Product/${product_id}` |
11 |
|
12 | const response = await fetch( |
13 | `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`, |
14 | { |
15 | method: "POST", |
16 | headers: { |
17 | "X-Shopify-Access-Token": auth.token, |
18 | "Content-Type": "application/json", |
19 | Accept: "application/json", |
20 | }, |
21 | body: JSON.stringify({ |
22 | query: `mutation ($input: ProductDeleteInput!) { productDelete(input: $input) { deletedProductId userErrors { field message } } }`, |
23 | variables: { input: { id } }, |
24 | }), |
25 | } |
26 | ) |
27 |
|
28 | if (!response.ok) { |
29 | throw new Error(`${response.status} ${await response.text()}`) |
30 | } |
31 |
|
32 | const { data, errors } = await response.json() |
33 | if (errors) { |
34 | throw new Error(JSON.stringify(errors)) |
35 | } |
36 | if (data.productDelete.userErrors.length) { |
37 | throw new Error(JSON.stringify(data.productDelete.userErrors)) |
38 | } |
39 |
|
40 | return data.productDelete |
41 | } |
42 |
|