Mechanic Get Mechanic
Task library

Delete all products

Use this task to quickly (and permanently) clear out your product list. Optionally, filter by a specific query, so as to only delete specific products from your store. Supports a test mode, allowing you to see exactly what products you can expect Mechanic to delete.

Try this task

When it runs

Occurs when a user manually triggers the task

What you choose

  • Query
  • Test mode
  • Live mode

Documentation

Use this task to quickly (and permanently) clear out your product list. Optionally, filter by a specific query, so as to only delete specific products from your store. Supports a test mode, allowing you to see exactly what products you can expect Mechanic to delete.

Use this task to quickly clear out your product list.

Caution: if you leave the query option empty, this task will delete every product in your store.

Use the "query" setting to get specific about what products you want to delete. Tick the "Test mode" checkbox to verify which products will be deleted: after saving, use the "Run task" button to run the product scan, and you'll receive a list of products that Mechanic would have deleted in live mode. Once you're ready, uncheck the "Test mode" box and check the "Live mode" box, save the task, then use "Run task" to permanently delete all matching products.

Example queries:

  • product_type:Shirt
  • tag:old

Learn more:

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
How task subscriptions work

Option types and flags

Task code

{% assign product_ids_and_titles = hash %}

{% assign cursor = nil %}
{% for n in (0..100) %}
  {% capture query %}
    query {
      products(
        first: 250
        after: {{ cursor | json }}
        sortKey: TITLE
        query: {{ options.query | json }}
      ) {
        pageInfo {
          hasNextPage
        }
        edges {
          cursor
          node {
            id
            title
          }
        }
      }
    }
  {% endcapture %}

  {% assign result = query | shopify %}

  {% if event.preview %}
    {% capture result_json %}
      {
        "data": {
          "products": {
            "pageInfo": {
              "hasNextPage": false
            },
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/Product/1234567890",
                  "title": "[sample product]"
                }
              }
            ]
          }
        }
      }
    {% endcapture %}

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

  {% for product_edge in result.data.products.edges %}
    {% assign product_ids_and_titles[product_edge.node.id] = product_edge.node.title %}
  {% endfor %}

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

{% if options.test_mode__boolean and options.live_mode__boolean %}
  {% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean == false and options.live_mode__boolean == false %}
  {% error "Please choose either test mode or live mode." %}
{% elsif options.test_mode__boolean %}
  {% log products_found_count: product_ids_and_titles.size, products_found: product_ids_and_titles %}
{% elsif options.live_mode__boolean %}
  {% for keyval in product_ids_and_titles %}
    {% action "shopify" %}
      mutation {
        productDelete(
          input: {
            id: {{ keyval[0] | json }}
          }
        ) {
          deletedProductId
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Mechanic Liquid reference

Default option values

Test mode
true
Live mode
false