Delete product or product variant metafields in bulk, with Mechanic.

Mechanic is a development platform for Shopify. :)

Delete product or product variant metafields in bulk

Use this task to delete all of your product or product variant metafields, optionally filtering by namespace or by namespace and key. This task will then delete all metafields found. This task 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 delete product metafields, delete product variant metafields, metafield namespace, metafield key, and test mode.

15-day free trial – unlimited tasks

Documentation

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

With no configuration, this task will delete all product or product variant 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.

Open source
View on GitHub to contribute to this task
Events
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
delete product metafields (boolean), delete product variant metafields (boolean), metafield namespace, metafield key, test mode (boolean)
Script
{% if options.delete_product_metafields__boolean and options.delete_product_variant_metafields__boolean %}
  {% error "You can only choose one option to target, either product metafields or product variant metafields, not both at the same time." %}
{% endif %}

{% if options.delete_product_metafields__boolean == false and options.delete_product_variant_metafields__boolean == false %}
  {% error "You must choose one option to target, either product metafields or product variant metafields." %}
{% endif %}

{% 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 {
        {% if options.delete_product_metafields__boolean %}
          products {
        {% elsif options.delete_product_variant_metafields__boolean %}
          productVariants {
        {% endif %}
          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 %}
    {% if options.delete_product_metafields__boolean %}
      {% 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 %}    
    {% elsif options.delete_product_variant_metafields__boolean %}
      {% capture objects_jsonl %}
        {% if options.metafield_key != blank %}
          {"__typename":"ProductVariant","id":"gid:\/\/shopify\/ProductVariant\/1234567890","metafield":{"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/1234567890"}}
        {% else %}
          {"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/0987654321","__parentId":"gid:\/\/shopify\/ProductVariant\/0987654321"}
        {% endif %}
      {% endcapture %}        
    {% endif %}

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

  {% endif %}

  
  {% assign metafield_ids = bulkOperation.objects | where: "__typename", "Metafield" | map: "id" %}
  
  {% if options.delete_product_metafields__boolean %}
    {% 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 %}
  {% elsif options.delete_product_variant_metafields__boolean %}
    {% assign metafield_ids = bulkOperation.objects | where: "__typename", "ProductVariant" | where: "metafield" | map: "metafield" | map: "id" | concat: metafield_ids %}
    {% assign product_variant_ids = bulkOperation.objects | where: "__typename", "Metafield" | map: "__parentId" | uniq %}
    {% assign product_variant_ids = bulkOperation.objects | where: "__typename", "ProductVariant" | where: "metafield" | map: "id" | concat: product_variant_ids %}
  {% endif %}

  {% if options.test_mode__boolean %}
    {% if options.delete_product_metafields__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 %}
    {% elsif options.delete_product_variant_metafields__boolean %}
      {% action "echo" metafields_found: metafield_ids.size, product_variants_with_metafields_found: product_variant_ids.size, metafield_ids: metafield_ids, product_variant_with_metafields_ids: product_variant_ids %}
    {% endif %}    

  {% else %}
    {% if options.delete_product_metafields__boolean %}
      {% log metafields_found: metafield_ids.size, products_with_metafields_found: product_ids.size, metafield_ids: metafield_ids, product_with_metafields_ids: product_ids %}
    {% elsif options.delete_product_variant_metafields__boolean %}
      {% log metafields_found: metafield_ids.size, product_variants_with_metafields_found: product_variant_ids.size, metafield_ids: metafield_ids, product_variant_with_metafields_ids: product_variant_ids %}
    {% endif %}        

    {% for metafield_id in metafield_ids %}
      {% action "shopify" %}
        mutation {
          metafieldDelete(
            input: {
              id: {{ metafield_id | json }}
            }
          ) {
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}

    {% if event.preview %}
      {% comment %}
        As a rule, preview events should render realistic
        action definitions. However, because Mechanic derives
        a task's required permissions from these definitions,
        and because a metafieldDelete mutation doesn't include
        anything indicating what kind of resource the
        metafield belongs to, we have to render an additional
        spurious preview-only mutation to nudge Mechanic into
        giving us write access to products.
      {% endcomment %}

      {% action "shopify" %}
        mutation {
          productUpdate(
            id: "gid://requesting/write_products"
          ) {
            thisMutationWillNotRunForLiveEvents
          }
        }
      {% endaction %}
    {% endif %}
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Delete product metafields
true
Test mode
true