Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

  • Delete product metafields
  • Delete variant metafields
  • Metafield namespace
  • Metafield key
  • Test mode
Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Delete product metafields
true
Test mode
true