Delete cancelled orders after 48 hours, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Delete cancelled orders after 48 hours

by Isaac Bowen (isaac@lightward.com)

When an order is cancelled, this task waits 48 hours, and then permanently deletes the order. Simple as that! (Please note: if you only want to archive the cancelled order, not delete it, try a different task!)

Runs when an order is updated and 48 hours after user/orders/delete_cancelled is triggered.

15-day free trial – unlimited tasks

Documentation

When an order is cancelled, this task waits 48 hours, and then permanently deletes the order. There is no undo for deleting an order, so make sure this is what you want!

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Events
when an order is updated (shopify/orders/updated)
48 hours after user/orders/delete_cancelled is triggered (user/orders/delete_cancelled+48.hours)
Script
{% if event.topic == "shopify/orders/updated" %}
  {% assign cache_key = "order-delete-scheduled-" | append: order.id %}
  {% assign order_qualifies = false %}
  {% if order.cancelled_at != blank and cache[cache_key] == blank %}
    {% assign order_qualifies = true %}
  {% endif %}

  {% if event.preview or order_qualifies %}
    {
      "action": {
        "type": "event",
        "options": {
          "topic": "user/orders/delete_cancelled",
          "data": {
            "order_id": {{ order.id | json }}
          }
        }
      }
    }

    {
      "action": {
        "type": "cache",
        "options": {
          "setex": {
            "key": {{ cache_key | json }},
            "value": true,
            "ttl": {{ 60 | times: 60 | times: 48 }}
          }
        }
      }
    }
  {% endif %}
{% elsif event.topic == "user/orders/delete_cancelled" %}
  {% assign order = shop.orders[event.data.order_id] %}
  {% if order.cancelled_at != blank or event.preview %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "delete",
          [
            "order",
            {{ event.data.order_id | json }}
          ]
        ]
      }
    }
  {% endif %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.