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 and Occurs every day at midnight (in local time). 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
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
date metafield namespace (required), date metafield key (required)
Code
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "delete",
        ["variant", {{ variant.id | json }}]
      ]
    }
  }
{% else %}
  {% assign today = "now" | date: "%Y-%m-%d" %}

  {% for product in shop.products.published %}
    {% if product.variants.size == 1 and product.variants[0].title == "Default Title" %}
      {% continue %}
    {% endif %}

    {% for variant in product.variants %}
      {% assign expiration_date = variant.metafields[options.date_metafield_namespace__required][options.date_metafield_key__required] %}
      {% if expiration_date != blank and expiration_date < today %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "delete",
              ["variant", {{ variant.id | json }}]
            ]
          }
        }
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more