Edits history of script submission #23718 for ' Delete Product (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Delete Product
     * Permanently delete a product and all its variants and media. This can't be undone.
     */
    export async function main(auth: RT.Shopify, product_id: string) {
      const id = product_id.startsWith("gid://")
        ? product_id
        : `gid://shopify/Product/${product_id}`
    
      const response = await fetch(
        `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
        {
          method: "POST",
          headers: {
            "X-Shopify-Access-Token": auth.token,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify({
            query: `mutation ($input: ProductDeleteInput!) { productDelete(input: $input) { deletedProductId userErrors { field message } } }`,
            variables: { input: { id } },
          }),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { data, errors } = await response.json()
      if (errors) {
        throw new Error(JSON.stringify(errors))
      }
      if (data.productDelete.userErrors.length) {
        throw new Error(JSON.stringify(data.productDelete.userErrors))
      }
    
      return data.productDelete
    }
    

    Submitted by hugo989 3 hours ago