Delete expired discounts after X days, with Mechanic.

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

Delete expired discounts after X days

Use this task to automatially keep your Discounts list clean by deleting any that have been expired for a configurable amount of days. Choose whether to delete automatic or code discounts, or both. Optionally, limit the deletions to discounts created by specific apps.

Runs Occurs every day at midnight (in local time) and Occurs when a user manually triggers the task. Configuration includes days after expiration to delete, delete automatic discounts, delete code discounts, and only delete discounts created by these apps.

15-day free trial – unlimited tasks

Documentation

Use this task to automatially keep your Discounts list clean by deleting any that have been expired for a configurable amount of days. Choose whether to delete automatic or code discounts, or both. Optionally, limit the deletions to discounts created by specific apps.

This task will run every night, and may also be run manually.

Notes:
- If limiting deletions to certain apps, the configured values must match app names exactly (e.g. "Shopify Web")
- This task cannot delete discounts created for use in Shopify Functions

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/scheduler/daily
mechanic/user/trigger
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
days after expiration to delete (number, required) , delete automatic discounts (boolean) , delete code discounts (boolean) , only delete discounts created by these apps (array)
Code
{% assign days_after_expiration = options.days_after_expiration_to_delete__number_required %}
{% assign delete_automatic_discounts = options.delete_automatic_discounts__boolean %}
{% assign delete_code_discounts = options.delete_code_discounts__boolean %}
{% assign qualifying_apps = options.only_delete_discounts_created_by_these_apps__array %}

{% unless delete_automatic_discounts or delete_code_discounts %}
  {% error "Select at least one discount type to delete: automatic or code." %}
{% endunless %}

{%- capture lookback_period -%}
  -{{ days_after_expiration | abs }} days
{%- endcapture -%}

{% assign cutoff_date = "now" | date: "%F", advance: lookback_period %}

{% log cutoff_date: cutoff_date %}

{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
  {% if delete_automatic_discounts %}
    {% comment %}
      -- get expired automatic discounts
      -- cannot filter by the creating app in the query, so that will be done afterwards if there are any configured
    {% endcomment %}

    {% assign cursor = nil %}
    {% assign discount_nodes = array %}

    {% for n in (1..100) %}
      {% capture query %}
        query {
          discountNodes(
            first: 250
            sortKey: ENDS_AT
            query: "method:automatic status:expired"
          ) {
            pageInfo {
              hasNextPage
              endCursor
            }
            nodes {
              id
              events(
                first: 1
                query: "action:create"
              ) {
                nodes {
                  appTitle
                }
              }
              discount {
                __typename
                ... on DiscountAutomaticBasic {
                  title
                  summary
                  status
                  createdAt
                  updatedAt
                  endsAt
                }
                ... on DiscountAutomaticBxgy {
                  title
                  summary
                  status
                  createdAt
                  updatedAt
                  endsAt
                }
                ... on DiscountAutomaticFreeShipping {
                  title
                  summary
                  status
                  createdAt
                  updatedAt
                  endsAt
                }
              }
            }
          }
        }
      {% endcapture %}

      {% assign result = query | shopify %}

      {% if event.preview %}
        {% capture result_json %}
          {
            "data": {
              "discountNodes": {
                "nodes": [
                  {
                    "id": "gid://shopify/DiscountAutomaticNode/1234567890",
                    "events": {
                      "nodes": [
                        {
                          "appTitle": {{ qualifying_apps.first | default: "Shopify Web"  | json }}
                        }
                      ]
                    },
                    "discount": {
                      "title": "ABCD",
                      "summary": "$5.00 off Alpha Shoe • Applies to each eligible item per order • One use per customer",
                      "status": "EXPIRED",
                      "endsAt": "2020-02-02T12:00:00Z"
                    }
                  }
                ]
              }
            }
          }
        {% endcapture %}

        {% assign result = result_json | parse_json %}
      {% endif %}

      {% comment %}
        -- verify that the code is expired and exceeds the cutoff date before marking for deletion
      {% endcomment %}

      {% for discount_node in result.data.discountNodes.nodes %}
        {% assign discount_ends_at = discount_node.discount.endsAt | date: "%F" %}

        {% unless discount_node.discount.status == "EXPIRED" and discount_ends_at <= cutoff_date %}
          {% continue %}
        {% endunless %}

        {% if qualifying_apps == blank or qualifying_apps contains discount_node.events.nodes.first.appTitle %}
          {% assign discount_nodes = discount_nodes | push: discount_node %}
        {% endif %}
      {% endfor %}

      {% if result.data.discountNodes.pageInfo.hasNextPage %}
        {% assign cursor = result.data.discountNodes.pageInfo.endCursor %}
      {% else %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if discount_nodes == blank %}
      {% log "No expired automatic discounts qualified to be deleted on this task run." %}

    {% else %}
      {% comment %}
        -- group discount nodes into groups of 250 for bulk deletion
      {% endcomment %}

      {% assign groups_of_discount_nodes = discount_nodes | in_groups_of: 250, fill_with: false %}

      {% for group_of_discount_nodes in groups_of_discount_nodes %}
        {% log
          message: "Deleting group of up to 250 automatic discounts.",
          count_of_discount_nodes_in_group: group_of_discount_nodes.size,
          discount_nodes_in_group: group_of_discount_nodes
        %}

        {% action "shopify" %}
          mutation {
            discountAutomaticBulkDelete(
              ids: {{ group_of_discount_nodes | map: "id" | graphql_arguments }}
            ) {
              job {
                id
              }
              userErrors {
                code
                extraInfo
                field
                message
              }
            }
          }
        {% endaction %}
      {% endfor %}
    {% endif %}
  {% endif %}

  {% if delete_code_discounts %}
    {% comment %}
      -- get expired code discounts
      -- cannot filter by the creating app in the query, so that will be done afterwards if there are any configured
    {% endcomment %}

    {% assign cursor = nil %}
    {% assign discount_nodes = array %}

    {% for n in (1..100) %}
      {% capture query %}
        query {
          discountNodes(
            first: 250
            sortKey: ENDS_AT
            query: "method:code status:expired"
          ) {
            pageInfo {
              hasNextPage
              endCursor
            }
            nodes {
              id
              events(
                first: 1
                query: "action:create"
              ) {
                nodes {
                  appTitle
                }
              }
              discount {
                __typename
                ... on DiscountCodeBasic {
                  title
                  summary
                  status
                  createdAt
                  updatedAt
                  endsAt
                }
                ... on DiscountCodeBxgy {
                  title
                  summary
                  status
                  createdAt
                  updatedAt
                  endsAt
                }
                ... on DiscountCodeFreeShipping {
                  title
                  summary
                  status
                  createdAt
                  updatedAt
                  endsAt
                }
              }
            }
          }
        }
      {% endcapture %}

      {% assign result = query | shopify %}

      {% if event.preview %}
        {% capture result_json %}
          {
            "data": {
              "discountNodes": {
                "nodes": [
                  {
                    "id": "gid://shopify/DiscountCodeNode/1234567890",
                    "events": {
                      "nodes": [
                        {
                          "appTitle": {{ qualifying_apps.first | default: "Shopify Web"  | json }}
                        }
                      ]
                    },
                    "discount": {
                      "title": "ABCD",
                      "summary": "$5.00 off Alpha Shoe • Applies to each eligible item per order • One use per customer",
                      "status": "EXPIRED",
                      "endsAt": "2020-02-02T12:00:00Z"
                    }
                  }
                ]
              }
            }
          }
        {% endcapture %}

        {% assign result = result_json | parse_json %}
      {% endif %}

      {% comment %}
        -- verify that the code is expired and exceeds the cutoff date before marking for deletion
      {% endcomment %}

      {% for discount_node in result.data.discountNodes.nodes %}
        {% assign discount_ends_at = discount_node.discount.endsAt | date: "%F" %}

        {% unless discount_node.discount.status == "EXPIRED" and discount_ends_at <= cutoff_date %}
          {% continue %}
        {% endunless %}

        {% if qualifying_apps == blank or qualifying_apps contains discount_node.events.nodes.first.appTitle %}
          {% assign discount_nodes = discount_nodes | push: discount_node %}
        {% endif %}
      {% endfor %}

      {% if result.data.discountNodes.pageInfo.hasNextPage %}
        {% assign cursor = result.data.discountNodes.pageInfo.endCursor %}
      {% else %}
        {% break %}
      {% endif %}
    {% endfor %}

    {% if discount_nodes == blank %}
      {% log "No expired code discounts qualified to be deleted on this task run." %}

    {% else %}
      {% comment %}
        -- group discount nodes into groups of 250 for bulk deletion
      {% endcomment %}

      {% assign groups_of_discount_nodes = discount_nodes | in_groups_of: 250, fill_with: false %}

      {% for group_of_discount_nodes in groups_of_discount_nodes %}
        {% log
          message: "Deleting group of up to 250 code discounts.",
          count_of_discount_nodes_in_group: group_of_discount_nodes.size,
          discount_nodes_in_group: group_of_discount_nodes
        %}

        {% action "shopify" %}
          mutation {
            discountCodeBulkDelete(
              ids: {{ group_of_discount_nodes | map: "id" | graphql_arguments }}
            ) {
              job {
                id
              }
              userErrors {
                code
                extraInfo
                field
                message
              }
            }
          }
        {% endaction %}
      {% endfor %}
    {% endif %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Days after expiration to delete
7
Delete code discounts
true