Delete product metafields in bulk, with Mechanic.

Mechanic is a development platform for Shopify. :)

Delete product metafields in bulk

by Isaac Bowen (team@usemechanic.com)

Use this task to delete all of your product metafields, optionally filtering by namespace or by namespace and key. This task will then delete all metafields found. Comes with a test mode – use this first to make sure you're deleting the right things.

Runs when a user triggers the task and when a bulk operation finishes. Configuration includes metafield namespace, metafield key, and test mode.

15-day free trial – unlimited tasks

Documentation

With no configuration, this task will delete all product metafields. Configure it with a metafield namespace to only delete metafields with that namespace, or add both a namespace and key to get even more specific. Run this task with the test mode option enabled, the first time, to make sure you're deleting the right material.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

Events
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
metafield namespace, metafield key, test mode (boolean)
Script
{% if options.metafield_namespace == blank and options.metafield_key != blank %}
  {% error "If you provide a metafield key, you must also provide a metafield namespace." %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      products {
        edges {
          node {
            __typename
            id
            {% if options.metafield_key != blank %}
              metafield(
                namespace: {{ options.metafield_namespace | json }}
                key: {{ options.metafield_key | json }}
              ) {
                __typename
                id
                description
                namespace
                key
                value
                valueType
              }
            {% else %}
              metafields(
                {% if options.metafield_namespace != blank %}
                  namespace: {{ options.metafield_namespace | json }}
                {% endif %}
              ) {
                edges {
                  node {
                    __typename
                    id
                    description
                    namespace
                    key
                    value
                    valueType
                  }
                }
              }
            {% endif %}
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture objects_jsonl %}
      {% if options.metafield_key != blank %}
        {"__typename":"Product","id":"gid:\/\/shopify\/Product\/1234567890","metafield":{"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/1234567890"}}
      {% else %}
        {"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/0987654321","__parentId":"gid:\/\/shopify\/Product\/0987654321"}
      {% endif %}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = objects_jsonl | parse_jsonl %}
  {% endif %}

  {% assign metafield_ids = bulkOperation.objects | where: "__typename", "Metafield" | map: "id" %}
  {% assign metafield_ids = bulkOperation.objects | where: "__typename", "Product" | where: "metafield" | map: "metafield" | map: "id" | concat: metafield_ids %}

  {% assign product_ids = bulkOperation.objects | where: "__typename", "Metafield" | map: "__parentId" | uniq %}
  {% assign product_ids = bulkOperation.objects | where: "__typename", "Product" | where: "metafield" | map: "id" | concat: product_ids %}

  {% if options.test_mode__boolean %}
    {% action "echo" metafields_found: metafield_ids.size, products_with_metafields_found: product_ids.size, metafield_ids: metafield_ids, product_with_metafields_ids: product_ids %}
  {% else %}
    {% log metafields_found: metafield_ids.size, products_with_metafields_found: product_ids.size, metafield_ids: metafield_ids, product_with_metafields_ids: product_ids %}

    {% for metafield_id in metafield_ids %}
      {% action "shopify" %}
        mutation {
          metafieldDelete(
            input: {
              id: {{ metafield_id | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Test mode
true