Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

Occurs when a user manually triggers the task

What you choose

  • Include open draft orders
  • Include invoice sent draft orders
  • Include closed draft orders
  • Only delete draft orders having this tag
  • Ignore draft orders having this tag

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).

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

Include open draft orders
false
Include invoice sent draft orders
false
Include closed draft orders
false