Delete non-purchased Neon Sign Customiser app generated products older than X days, with Mechanic.

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

Delete non-purchased Neon Sign Customiser app generated products older than X days

Use this task to automatically delete non-purchased products created by Neon Sign Customiser. Set the number of days old products should be before Mechanic deletes a product.

Runs Occurs when a user manually triggers the task. Configuration includes products must be x number of days old, test mode, live mode, and run daily.

15-day free trial – unlimited tasks

Documentation

Use this task to automatically delete non-purchased products created by Neon Sign Customiser. Set the number of days old products should be before Mechanic deletes a product.

Tick the "Test mode" checkbox to verify which products will be deleted: after saving, use the "Run task" button to run the product scan, and you'll receive a list of products that Mechanic would have deleted in live mode.

Once you're ready, uncheck the "Test mode" box and check the "Live mode" box, save the task, then use "Run task" to permanently delete all matching products.

Check the run daily option to have the task run at 12AM daily.

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
{% if options.run_daily__boolean %}
 mechanic/scheduler/daily
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
products must be x number of days old (number, , required), test mode (boolean), live mode (boolean), run daily (boolean)
Code
{% comment %}
  Options ordering:
  {{ options.products_must_be_x_number_of_days_old__number__required }} 
  {{ options.test_mode__boolean }}
  {{ options.live_mode__boolean }}  
{% endcomment %}

{% assign products_must_be_x_number_of_seconds_old = options.products_must_be_x_number_of_days_old__number__required | times: 24 | times: 60 | times: 60 %}

{% assign products_age_threshold_s = "now" | date: "%s" | minus: products_must_be_x_number_of_seconds_old %}

{% assign products_age_threshold_human = products_age_threshold_s | date: "%Y-%m-%d %H:%M %:z" %}

{% assign products_age_threshold_date = products_age_threshold_s | date: "%Y-%m-%d" %}

{% log %}{{ "Threshold for deleteing products: " | append: products_age_threshold_human | json }}{% endlog %}

{% assign product_ids_and_titles = hash %}

{% capture product_query -%}
  -tag:npc_ordered product_type:'Custom Neon' created_at:<{{ products_age_threshold_date }}
{% endcapture %}

{% assign cursor = nil %}
{% for n in (0..100) %}
  {% capture query %}
    query {
      products(
        first: 250
        after: {{ cursor | json }}
        sortKey: TITLE
        query: {{ product_query | json }}
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            id
            title
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "products": {
            "pageInfo": {
              "hasNextPage": false
            },
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/Product/1234567890",
                  "title": "[sample product]"
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

    {% assign result = result_json | parse_json %}
  {% endif %}

  {% for product_edge in result.data.products.edges %}
    {% assign product_ids_and_titles[product_edge.node.id] = product_edge.node.title %}
  {% endfor %}

  {% if result.data.products.pageInfo.hasNextPage %}
    {% assign cursor = result.data.products.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% if options.test_mode__boolean and options.live_mode__boolean %}
  {% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean == false and options.live_mode__boolean == false %}
  {% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean %}
  {% log products_found_count: product_ids_and_titles.size, products_found: product_ids_and_titles %}
{% elsif options.live_mode__boolean %}
  {% for keyval in product_ids_and_titles %}
    {% action "shopify" %}
      mutation {
        productDelete(
          input: {
            id: {{ keyval[0] | json }}
          }
        ) {
          deletedProductId
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Products must be x number of days old
7
Test mode
true