Edits history of script submission #23724 for ' Delete Metafield (shopify)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Delete Metafield
     * Delete a metafield, identified by its owner GID (e.g. `gid://shopify/Product/123`), namespace and key. deletedMetafields contains null when nothing matched.
     */
    export async function main(
      auth: RT.Shopify,
      owner_id: string,
      namespace: string,
      key: string
    ) {
      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 ($metafields: [MetafieldIdentifierInput!]!) { metafieldsDelete(metafields: $metafields) { deletedMetafields { ownerId namespace key } userErrors { field message } } }`,
            variables: { metafields: [{ ownerId: owner_id, namespace, key }] },
          }),
        }
      )
    
      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.metafieldsDelete.userErrors.length) {
        throw new Error(JSON.stringify(data.metafieldsDelete.userErrors))
      }
    
      return data.metafieldsDelete
    }
    

    Submitted by hugo989 3 hours ago