Mechanic is a development and ecommerce automation platform for Shopify. :)
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.
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.
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?)
mechanic/user/trigger {% if options.run_daily__boolean %} mechanic/scheduler/daily {% endif %}
{% 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 %}