0

Delete Metafield

by
Published Nov 8, 2023

Delete a metafield, identified by its owner GID (e.g. `gid://shopify/Product/123`), namespace and key. deletedMetafields contains null when nothing matched.

Script shopify Verified

The script

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

3
/**
4
 * Delete Metafield
5
 * Delete a metafield, identified by its owner GID (e.g. `gid://shopify/Product/123`), namespace and key. deletedMetafields contains null when nothing matched.
6
 */
7
export async function main(
8
  auth: RT.Shopify,
9
  owner_id: string,
10
  namespace: string,
11
  key: string
12
) {
13
  const response = await fetch(
14
    `https://${auth.store_name}.myshopify.com/admin/api/2026-10/graphql.json`,
15
    {
16
      method: "POST",
17
      headers: {
18
        "X-Shopify-Access-Token": auth.token,
19
        "Content-Type": "application/json",
20
        Accept: "application/json",
21
      },
22
      body: JSON.stringify({
23
        query: `mutation ($metafields: [MetafieldIdentifierInput!]!) { metafieldsDelete(metafields: $metafields) { deletedMetafields { ownerId namespace key } userErrors { field message } } }`,
24
        variables: { metafields: [{ ownerId: owner_id, namespace, key }] },
25
      }),
26
    }
27
  )
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`)
31
  }
32

33
  const { data, errors } = await response.json()
34
  if (errors) {
35
    throw new Error(JSON.stringify(errors))
36
  }
37
  if (data.metafieldsDelete.userErrors.length) {
38
    throw new Error(JSON.stringify(data.metafieldsDelete.userErrors))
39
  }
40

41
  return data.metafieldsDelete
42
}
43

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 metafield by its ID
    7
     * Deletes a metafield by its ID.
    8
     */
    9
    export async function main(
    10
      auth: Shopify,
    11
      api_version: string = "2023-10",
    12
      metafield_id: string
    13
    ) {
    14
      const url = new URL(
    15
        `https://${auth.store_name}.myshopify.com/admin/api/${api_version}/metafields/${metafield_id}.json`
    16
      );
    17
    
    
    18
      const response = await fetch(url, {
    19
        method: "DELETE",
    20
        headers: {
    21
          "X-Shopify-Access-Token": auth.token,
    22
        },
    23
        body: undefined,
    24
      });
    25
      if (!response.ok) {
    26
        const text = await response.text();
    27
        throw new Error(`${response.status} ${text}`);
    28
      }
    29
      return await response.json();
    30
    }
    31