Find duplicate SKUs, with Mechanic.

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

Find duplicate SKUs

This task scans your entire product list, looking for duplicate SKUs using exact matching, and returning the list of duplicate SKUs (and the associated product variant IDs) if any are found.

Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes exclude products that do not track inventory.

15-day free trial – unlimited tasks

Documentation

This task scans your entire product list, looking for duplicate SKUs using exact matching, and returning the list of duplicate SKUs (and the associated product variant IDs) if any are found.

Note: SKUs that are identical apart from being uppercase/lowercase, for example, do not count as exact matches, and would not be considered duplicates by this task.

"Exclude products that do not track inventory" will exclude any product whose inventory is not tracked in Shopify.

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
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
exclude products that do not track inventory (boolean)
Code
{% if event.topic == "mechanic/user/trigger" %}
  {% assign search_query = "-sku:''" %}

  {% if options.exclude_products_that_do_not_track_inventory__boolean %}
    {% assign search_query = search_query | append: " managed:true" %}
  {% endif %}

  {% capture bulk_operation_query %}
    query {
      productVariants(
        query: {{ search_query | json }}
      ) {
        edges {
          node {
            __typename
            id
            sku
          }
        }
      }
    }
  {% endcapture %}

  {% action "shopify" %}
    mutation {
      bulkOperationRunQuery(
        query: {{ bulk_operation_query | json }}
      ) {
        bulkOperation {
          id
          status
        }
        userErrors {
          field
          message
        }
      }
    }
  {% endaction %}

{% elsif event.topic == "mechanic/shopify/bulk_operation" %}
  {% if event.preview %}
    {% capture jsonl_string %}
      {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/1234567890","sku":"ABC123"}
      {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/2345678901","sku":"ABC123"}
      {"__typename":"ProductVariant","id":"gid://shopify/ProductVariant/3456789012","sku":"DEF456"}
    {% endcapture %}

    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = jsonl_string | parse_jsonl %}
  {% endif %}

  {% assign variants = bulkOperation.objects | where: "__typename", "ProductVariant" %}

  {% assign skus_by_variant_id = hash %}

  {% for variant in variants %}
    {% assign skus_by_variant_id[variant.id] = variant.sku %}
  {% endfor %}

  {% assign all_skus = skus_by_variant_id | values %}
  {% assign unique_skus = all_skus | uniq %}

  {% if all_skus.size == unique_skus.size %}
    {% comment %}
      No exact duplicate SKUs, so let's end execution early
    {% endcomment %}
    {% log message: "No duplicate SKUs found! :)" %}
    {% break %}
  {% endif %}

  {% assign variant_ids_by_duplicate_sku = hash %}

  {% for pair in skus_by_variant_id %}
    {% assign variant_id = pair[0] %}
    {% assign sku = pair[1] %}
    {% assign sku_count = 0 %}

    {% for some_sku in all_skus %}
      {% if some_sku == sku %}
        {% assign sku_count = sku_count | plus: 1 %}
      {% endif %}
    {% endfor %}

    {% if sku_count == 1 %}
      {% comment %}
        Ignore SKUs that only occur once.
      {% endcomment %}
      {% continue %}
    {% endif %}

    {% assign variant_ids_by_duplicate_sku[sku] = variant_ids_by_duplicate_sku[sku] | default: array | push: variant_id %}
  {% endfor %}

  {% assign duplicate_skus = variant_ids_by_duplicate_sku | keys %}

  {% log
    message: "Found duplicate SKU(s)",
    duplicate_skus_size: duplicate_skus.size,
    duplicate_skus: duplicate_skus,
    variant_ids_by_duplicate_sku: variant_ids_by_duplicate_sku
  %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more