Delete draft orders older than 30 days, with Mechanic.

Mechanic is an automation development platform for Shopify. :)

Delete draft orders older than 30 days

by Isaac Bowen (team@usemechanic.com)

Keep your draft order list tidy - use this task to automatically delete orders that are older than a configurable number of days.

Runs when a user triggers the task. Configuration includes days to wait before deleting a draft order, test mode, and run daily.

15-day free trial – unlimited tasks

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
days to wait before deleting a draft order (number, required), test mode (boolean), run daily (boolean)
Script
{% assign now_s = "now" | date: "%s" | times: 1 %}
{% assign x_days_ago_interval_s = options.days_to_wait_before_deleting_a_draft_order__number_required | times: 24 | times: 60 | times: 60 %}
{% assign x_days_ago_s = now_s | minus: x_days_ago_interval_s %}
{% assign x_days_ago_date = x_days_ago_s | date: "%Y-%m-%d" %}

{% assign ids_to_delete = array %}

{% assign cursor = nil %}

{% for n in (0..100) %}
  {% capture query %}
    query {
      draftOrders(
        first: 250
        after: {{ cursor | json }}
        query: {{ "created_at:<" | append: x_days_ago_date | json }}
        sortKey: ID
      ) {
        edges {
          cursor
          node {
            id
            name
            createdAt
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% for draftOrder_edge in result.data.draftOrders.edges %}
    {% assign draftOrder = draftOrder_edge.node %}

    {% assign ids_to_delete[ids_to_delete.size] = draftOrder.id %}
  {% endfor %}

  {% if result.data.draftOrders.pageInfo.hasNextPage %}
    {% assign cursor = result.data.draftOrders.edges.last.cursor %}
  {% else %}
    {% break %}
  {% endif %}
{% endfor %}

{% if event.preview %}
  {% assign ids_to_delete[0] = "gid://shopify/DraftOrder/1234567890" %}
{% endif %}

{% if options.test_mode__boolean %}
  {% capture message -%}
    Test mode: found {{ ids_to_delete.size }} qualifying draft order(s). Disable test mode and run again to delete draft orders that qualify during that run.
  {%- endcapture %}

  {% action "echo" message %}
{% else %}
  {% for id in ids_to_delete %}
    {% action "shopify" %}
      mutation {
        draftOrderDelete(
          input: {
            id: {{ id | json }}
          }
        ) {
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Days to wait before deleting a draft order
30
Test mode
true