Delete variants having a metafield date that has passed, with Mechanic.

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

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.

Runs Occurs when a user manually triggers the task, Occurs every day at midnight (in local time), and Occurs when a bulk operation is completed. Configuration includes date metafield namespace and date metafield key.

15-day free trial – unlimited tasks

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.

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/scheduler/daily
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
date metafield namespace (required), date metafield key (required)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more