Mechanic Get Mechanic
Task library

Delete the oldest x products from a specific collection

Either triggered manually, or configured to run daily, this task will look for the oldest products in the collection of your choice, and delete as many of them as you wish.

Try this task

When it runs

Occurs when a user manually triggers the task

What you choose

  • Collection ID
  • Number of products to delete at once
  • Test mode
  • Run daily

Documentation

Either triggered manually, or configured to run daily, this task will look for the oldest products in the collection of your choice, and delete as many of them as you wish.

IMPORTANT: When first configuring this task, run it manually once in "Test mode" to see a list of which products it would delete. Once verfied, be sure to uncheck this option to have the task make the deletions going forward.

Notes:
- The products will be deleted asycnronously by Shopify, which means they may appear in the products list of admin for a bit after a task run.
- This task requires a collection ID - learn how to locate common resource IDs.

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
{% if options.run_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
How task subscriptions work

Option types and flags

Task code

{% assign collection_id = options.collection_id__number_required %}
{% assign number_of_products_to_delete_at_once = options.number_of_products_to_delete_at_once__number_required %}
{% assign test_mode = options.test_mode__boolean %}

{% if number_of_products_to_delete_at_once > 250 %}
  {% error "This task only supports deleting up to 250 products in a single task run." %}
{% endif %}

{% comment %}
  -- get X oldest products that are in the collection
{% endcomment %}

{% capture query %}
  query {
    collection(id: {{ collection_id | prepend: "gid://shopify/Collection/" | json }}) {
      products(
        first: {{ number_of_products_to_delete_at_once }}
        sortKey: CREATED
      ) {
        nodes {
          id
          title
          createdAt
        }
      }
    }
  }
{% endcapture %}

{% assign result = query | shopify %}

{% if event.preview %}
  {% capture result_json %}
    {
      "data": {
        "collection": {
          "products": {
            "nodes": [
              {
                "id": "gid://shopify/Product/1234567890",
                "createdAt": "2020-02-02T01:01:00Z"
              }
            ]
          }
        }
      }
    }
  {% endcapture %}

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

{% assign collection = result.data.collection %}
{% assign products = collection.products.nodes %}

{% if collection == blank %}
  {% error "Unable to find a collection by the configured ID in this shop." %}
{% endif %}

{% log products_to_delete: products %}

{% if test_mode %}
  {% break %}
{% endif %}

{% comment %}
  -- delete all returned products asynchronously in case they have many variants and/or media (which could cause an API timeout)
{% endcomment %}

{% for product in collection.products.nodes %}
  {% action "shopify" %}
    mutation {
      productDelete(
        input: {
          id: {{ product.id | json }}
        }
        synchronous: false
      ) {
        productDeleteOperation {
          id
          product {
            legacyResourceId
            title
          }
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}
{% endfor %}
Mechanic Liquid reference