Delete draft orders with Mechanic.

Mechanic is an automation and development platform for Shopify. :)

Delete draft orders

by Isaac Bowen (team@usemechanic.com)

Use this task to clear out your draft orders, on demand. Filter by draft order status (open, invoice sent, closed), or by draft order tags.

Runs when a user triggers the task. Configuration includes include open draft orders, include invoice sent draft orders, include closed draft orders, only delete draft orders having this tag, and ignore draft orders having this tag.

15-day free trial – unlimited tasks

Documentation

Important: Be sure to configure this task before running! You may filter by draft order status (open, invoice sent, or closed), and by draft order tag (to selectively delete, or to selectively ignore).

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 a user triggers the task (mechanic/user/trigger)
Options
include open draft orders (boolean), include invoice sent draft orders (boolean), include closed draft orders (boolean), only delete draft orders having this tag, ignore draft orders having this tag
Script
{% if event.preview %}
  {
    "action": {
      "type": "shopify",
      "options": [
        "delete",
        [
          "draft_order",
          1234567890
        ]
      ]
    }
  }
{% else %}
  {% assign selectors = "" %}

  {% if options.include_open_draft_orders__boolean %}
    {% assign selectors = selectors | append: ",open" %}
  {% endif %}
  {% if options.include_invoice_sent_draft_orders__boolean %}
    {% assign selectors = selectors | append: ",invoice_sent" %}
  {% endif %}
  {% if options.include_closed_draft_orders__boolean %}
    {% assign selectors = selectors | append: ",closed" %}
  {% endif %}

  {% assign selectors = selectors | slice: 1, 100 | split: "," %}
  {"log": {{ selectors | json }}}
  {"log": {{ options | json }}}

  {% for selector in selectors %}
    {% for draft_order in shop.draft_orders[selector] %}
      {% assign draft_order_qualifies = true %}
      {% assign draft_order_tags = draft_order.tags | split: ", " %}

      {% if options.only_delete_draft_orders_having_this_tag != blank %}
        {% unless draft_order_tags contains options.only_delete_draft_orders_having_this_tag %}
          {% assign draft_order_qualifies = false %}
        {% endunless %}
      {% endif %}

      {% if options.ignore_draft_orders_having_this_tag != blank %}
        {% if draft_order_tags contains options.ignore_draft_orders_having_this_tag %}
          {% assign draft_order_qualifies = false %}
        {% endif %}
      {% endif %}

      {% if draft_order_qualifies %}
        {
          "action": {
            "type": "shopify",
            "options": [
              "delete",
              [
                "draft_order",
                {{ draft_order.id | json }}
              ]
            ]
          }
        }
      {% endif %}
    {% endfor %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.