Delete the oldest x products from a specific collection, with Mechanic.

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

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.

Runs Occurs when a user manually triggers the task. Configuration includes collection id, number of products to delete at once, test mode, and run daily.

15-day free trial – unlimited tasks

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.

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
{% if options.run_daily__boolean %}
  mechanic/scheduler/daily
{% endif %}
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
collection id (number, required), number of products to delete at once (number, required), test mode (boolean), run daily (boolean)
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 %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more