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

Mechanic is a development and ecommerce automation platform for Shopify. :)

Delete product or product variant metafields in bulk

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

Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes delete product metafields, delete variant metafields, metafield namespace, metafield key, and test mode.

15-day free trial – unlimited tasks

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, 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.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/user/trigger
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
delete product metafields (boolean), delete variant metafields (boolean), metafield namespace, metafield key, test mode (boolean)
Code
{% assign delete_product_metafields = options.delete_product_metafields__boolean %}
{% assign delete_variant_metafields = options.delete_variant_metafields__boolean %}
{% assign metafield_namespace = options.metafield_namespace %}
{% assign metafield_key = options.metafield_key %}
{% assign test_mode = options.test_mode__boolean %}

{% if delete_product_metafields and delete_variant_metafields %}
  {% error "You can only choose one option to target, either product metafields or variant metafields, not both at the same time." %}
{% endif %}

{% unless delete_product_metafields or delete_variant_metafields %}
  {% error "You must choose one option to target, either product metafields or variant metafields." %}
{% endunless %}

{% if metafield_namespace == blank and metafield_key != blank %}
  {% error "If you provide a metafield key, you must also provide a metafield namespace." %}
{% endif %}

{% if delete_product_metafields %}
  {% assign object_type = "Product" %}
  {% assign query_type = "products" %}

{% elsif delete_variant_metafields %}
  {% assign object_type = "ProductVariant" %}
  {% assign query_type = "productVariants" %}
{% endif %}

{% if event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
      query {
        {{ query_type }} {
          edges {
            node {
              __typename
              id
              {% if metafield_key != blank %}
                metafield(
                  namespace: {{ metafield_namespace | json }}
                  key: {{ metafield_key | json }}
                ) {
                  __typename
                  id
                  description
                  namespace
                  key
                  value
                  type
                }
              {% else %}
                metafields
                  {% if metafield_namespace != blank %}
                    (namespace: {{ metafield_namespace | json }})
                  {% endif %}
                  {
                  edges {
                    node {
                      __typename
                      id
                      description
                      namespace
                      key
                      value
                      type
                    }
                  }
                }
              {% 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 %}
      {"__typename":"{{ object_type }}","id":"gid:\/\/shopify\/Product\/1234567890","metafield":{"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/1234567890"}}
      {"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/2345678901","__parentId":"gid:\/\/shopify\/{{ object_type }}\/1234567890"}
    {% endcapture %}

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

  {% assign metafields_to_delete = array %}

  {% if metafield_key != blank %}
    {% comment %}
      -- metafield key was configured, thus the metafields will only be on the product or variant objects in the returned bulk objects
    {% endcomment %}

    {% assign objects_with_metafield
      = bulkOperation.objects
      | where: "__typename", object_type
      | where: "metafield"
    %}

    {% for object in objects_with_metafield %}
      {% assign metafield_to_delete = hash %}
      {% assign metafield_to_delete["ownerId"] = object.id %}
      {% assign metafield_to_delete["namespace"] = metafield_namespace %}
      {% assign metafield_to_delete["key"] = metafield_key %}
      {% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}
    {% endfor %}

  {% else %}
    {% comment %}
      -- metafield key was not configured, thus the metafields will be separated in the returned bulk objects
    {% endcomment %}

    {% assign metafields = bulkOperation.objects | where: "__typename", "Metafield" %}

    {% for metafield in metafields %}
      {% assign metafield_to_delete = hash %}
      {% assign metafield_to_delete["ownerId"] = metafield.__parentId %}
      {% assign metafield_to_delete["namespace"] = metafield.namespace %}
      {% assign metafield_to_delete["key"] = metafield.key %}
      {% assign metafields_to_delete = metafields_to_delete | push: metafield_to_delete %}
    {% endfor %}
  {% endif %}

  {% log %}
    "Found {{ metafields_to_delete.size }} metafields to delete."
  {% endlog %}

  {% if metafields_to_delete != blank %}
    {% if test_mode %}
      {% log "Task has test mode enabled. No metafields will be deleted on this task run." %}
      {% break %}
    {% endif %}

    {% comment %}
      -- metafields may be deleted in batches of 250
    {% endcomment %}

    {% assign groups_of_metafields_to_delete = metafields_to_delete | in_groups_of: 250, fill_with: false %}

    {% for group_of_metafields_to_delete in groups_of_metafields_to_delete %}
      {% action "shopify" %}
        mutation {
          metafieldsDelete(
            metafields: {{ group_of_metafields_to_delete | graphql_arguments }}
          ) {
            deletedMetafields {
              ownerId
              namespace
              key
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Delete product metafields
true
Test mode
true