Clean up draft orders, with Mechanic.

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

Clean up draft orders

Use this task to quickly delete draft orders from your account, optionally filtering by status, or by draft order age. Useful for keeping things tidy. :)

Runs Occurs when a user manually triggers the task. Configuration includes delete open draft orders, delete draft orders that have invoices sent, delete completed draft orders, minimum draft order age in days, and run daily at midnight.

15-day free trial – unlimited tasks

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_at_midnight__boolean %}
  mechanic/scheduler/daily
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
delete open draft orders (boolean), delete draft orders that have invoices sent (boolean), delete completed draft orders (boolean), minimum draft order age in days (number), run daily at midnight (boolean)
Code
{% if event.preview %}
  {% if options.delete_open_draft_orders__boolean or options.delete_draft_orders_that_have_invoices_sent__boolean or options.delete_completed_draft_orders__boolean %}
    {
      "action": {
        "type": "shopify",
        "options": [
          "delete",
          [
            "draft_order",
            1234567890
          ]
        ]
      }
    }
  {% else %}
    {"error": "Please choose at least one draft order status to delete. :)"}
  {% endif %}
{% else %}
  {% assign maximum_created_at_s = nil %}
  {% if options.minimum_draft_order_age_in_days__number != blank %}
    {% assign minimum_draft_order_age_s = options.minimum_draft_order_age_in_days__number | times: 24 | times: 60 | times: 60 %}
    {% assign maximum_created_at_s = "now" | date: "%s" | minus: minimum_draft_order_age_s %}
  {% endif %}

  {% assign statuses = "open,invoice_sent,completed" | split: "," %}

  {% for status in statuses %}
    {% if status == "open" and options.delete_open_draft_orders__boolean != true %}
      {% continue %}
    {% elsif status == "invoice_sent" and options.delete_draft_orders_that_have_invoices_sent__boolean != true %}
      {% continue %}
    {% elsif status == "completed" and options.delete_completed_draft_orders__boolean != true %}
      {% continue %}
    {% endif %}

    {% for draft_order in shop.draft_orders[status] %}
      {% if maximum_created_at_s != nil %}
        {% assign draft_order_created_at_s = draft_order.created_at | date: "%s" | times: 1 %}
        {% if draft_order_created_at_s > maximum_created_at_s %}
          {% continue %}
        {% endif %}
      {% endif %}

      {
        "action": {
          "type": "shopify",
          "options": [
            "delete",
            [
              "draft_order",
              {{ draft_order.id | json }}
            ]
          ]
        }
      }
    {% endfor %}
  {% endfor %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more