Delete customer metafields in bulk, with Mechanic.

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

Delete customer metafields in bulk

With no configuration, this task will delete all customer metafields in bulk. 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 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
metafield namespace, metafield key, test mode (boolean)
Code
{% assign metafield_namespace = options.metafield_namespace %}
{% assign metafield_key = options.metafield_key %}
{% assign test_mode = options.test_mode__boolean %}

{% if metafield_namespace == blank and 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 {
        customers {
          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":"Customer","id":"gid:\/\/shopify\/Customer\/1234567890","metafield":{"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/1234567890"}}
      {"__typename":"Metafield","id":"gid:\/\/shopify\/Metafield\/2345678901","__parentId":"gid:\/\/shopify\/Customer\/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 customer object in the returned bulk objects
    {% endcomment %}

    {% assign customers_with_metafield
      = bulkOperation.objects
      | where: "__typename", "Customer"
      | where: "metafield"
    %}

    {% for customer in customers_with_metafield %}
      {% assign metafield_to_delete = hash %}
      {% assign metafield_to_delete["ownerId"] = customer.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
Test mode
true