0

Delete Discount Code

by
Published Nov 8, 2023

Permanently delete a code discount and all of its codes (numeric ID or DiscountCodeNode GID). Needs the write_discounts scope.

Script shopify Verified

The script

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

3
/**
4
 * Delete Discount Code
5
 * Permanently delete a code discount and all of its codes (numeric ID or DiscountCodeNode GID). Needs the write_discounts scope.
6
 */
7
export async function main(auth: RT.Shopify, discount_id: string) {
8
  const id = discount_id.startsWith("gid://")
9
    ? discount_id
10
    : `gid://shopify/DiscountCodeNode/${discount_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 ($id: ID!) { discountCodeDelete(id: $id) { deletedCodeDiscountId userErrors { field message } } }`,
23
        variables: { 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.discountCodeDelete.userErrors.length) {
37
    throw new Error(JSON.stringify(data.discountCodeDelete.userErrors))
38
  }
39

40
  return data.discountCodeDelete
41
}
42

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Shopify = {
    2
      token: string;
    3
      store_name: string;
    4
    };
    5
    /**
    6
     * Deletes a discount code
    7
     * Deletes a discount code
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      price_rule_id: string,
    13
      discount_code_id: string
    14
    ) {
    15
      const url = new URL(
    16
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/price_rules/${price_rule_id}/discount_codes/${discount_code_id}.json`
    17
      );
    18
    
    
    19
      const response = await fetch(url, {
    20
        method: "DELETE",
    21
        headers: {
    22
          "X-Shopify-Access-Token": auth.token,
    23
        },
    24
        body: undefined,
    25
      });
    26
      if (!response.ok) {
    27
        const text = await response.text();
    28
        throw new Error(`${response.status} ${text}`);
    29
      }
    30
      return await response.text();
    31
    }
    32