Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a bulk operation is completed

What you choose

Exclude products that do not track inventory

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.

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
mechanic/shopify/bulk_operation
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference