Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

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

What you choose

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

Default option values

Test mode
true