Delete draft orders, with Mechanic.

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

Delete draft orders

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 Occurs when a user manually 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

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.

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, 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
Tasks use subscriptions to sign up for specific kinds of events. Learn more
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
Code
{% 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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more