Mechanic Get Mechanic
Task library

Delete variants having a metafield date that has passed

Do you sell tickets to recurring events? Do you use a new product variant for each one? Use this task to regularly scan your catalog for product variants that have a date metafield, and to delete variants when their date has passed.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs every day at midnight (in local time)
  • Occurs when a bulk operation is completed

What you choose

  • Date metafield namespace
  • Date metafield key

Documentation

Do you sell tickets to recurring events? Do you use a new product variant for each one? Use this task to regularly scan your catalog for product variants that have a date metafield, and to delete variants when their date has passed.

Run this task manually to immediately scan your product catalog for outdated variants, and delete them. This task will also run nightly, at midnight in your store's timezone.

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/scheduler/daily
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task code

{% assign date_metafield_namespace = options.date_metafield_namespace__required %}
{% assign date_metafield_key = options.date_metafield_key__required %}

{% assign today = "now" | date: "%Y-%m-%d" %}

{% if event.topic == "mechanic/user/trigger" or event.topic == "mechanic/scheduler/daily" %}
  {% comment %}
    -- get all active, published, multi-variant products in the shop
  {% endcomment %}

  {% capture bulk_operation_query %}
    query {
      products(
        query: "status:active published_status:published has_only_default_variant:false"
      ) {
        edges {
          node {
            __typename
            id
            variants {
              edges {
                node {
                  __typename
                  id
                  metafield(
                    namespace: {{ date_metafield_namespace | json }}
                    key: {{ date_metafield_key | json }}
                  ) {
                    value
                  }
                }
              }
            }
          }
        }
      }
    }
  {% 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 jsonl_string %}
      {"__typename":"Product","id":"gid://shopify/Product/1234567890"}
      {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/1234567890","metafield":{"value":"2020-02-02"},"__parentId":"gid://shopify/Product/1234567890"}
      {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/2345678901","__parentId":"gid://shopify/Product/1234567890"}
    {% endcapture %}

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

  {% assign products = bulkOperation.objects | where: "__typename", "Product" %}
  {% assign bulk_variants = bulkOperation.objects | where: "__typename", "ProductVariant" %}

  {% comment %}
    -- for each product, qualify and delete variants in bulk by metafield value
  {% endcomment %}

  {% for product in products %}
    {% assign variants = bulk_variants | where: "__parentId", product.id %}

    {% assign variant_ids_to_delete = array %}

    {% for variant in variants %}
      {% if variant.metafield != blank and variant.metafield.value < today %}
        {% assign variant_ids_to_delete = variant_ids_to_delete | push: variant.id %}
      {% endif %}
    {% endfor %}

    {% if variant_ids_to_delete != blank %}
      {% action "shopify" %}
        mutation {
          productVariantsBulkDelete(
            productId: {{ product.id | json }}
            variantsIds: {{ variant_ids_to_delete | json }}
          ) {
            product {
              id
              title
            }
            userErrors {
              code
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference