Mechanic Get Mechanic
Task library

Reset all inventory levels to a single level, in bulk

A utility task, this one is useful for those one-off moments when your inventory needs a clean slate. Run this task manually to reset all of your inventory, across your entire store and for all locations, to the single level of your choice. No exceptions, no filters.

Try this task

When it runs

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

What you choose

Reset all inventory items to this level

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

{% assign reset_level = options.reset_all_inventory_items_to_this_level__number_required  %}

{% if event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      inventoryItems {
        edges {
          node {
            __typename
            id
            inventoryLevels {
              edges {
                node {
                  __typename
                  id
                  location {
                    id
                  }
                  quantities(names: "available") {
                    name
                    quantity
                  }
                }
              }
            }
          }
        }
      }
    }
  {% 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 bulkOperation_objects_jsonl %}
      {"__typename":"InventoryItem","id":"gid:\/\/shopify\/InventoryItem\/1234567890"}
      {"__typename":"InventoryLevel","location":{"id":"gid:\/\/shopify\/Location\/1234567890"},"quantities":[{"name":"available","quantity":{{ reset_level | minus: 5 }}}],"__parentId":"gid:\/\/shopify\/InventoryItem\/1234567890"}
    {% endcapture %}

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

  {% assign bulk_inventory_items = bulkOperation.objects | where: "__typename", "InventoryItem" %}
  {% assign bulk_inventory_levels = bulkOperation.objects | where: "__typename", "InventoryLevel" %}

  {% assign inventory_adjustments = array %}

  {% for inventory_item in bulk_inventory_items %}
    {% assign inventory_levels = bulk_inventory_levels | where: "__parentId", inventory_item.id %}

    {% for inventory_level in inventory_levels %}
      {% if inventory_level.quantities.first.quantity != reset_level %}
        {% assign inventory_adjustment = hash %}
        {% assign inventory_adjustment["inventoryItemId"] = inventory_item.id %}
        {% assign inventory_adjustment["locationId"] = inventory_level.location.id %}
        {% assign inventory_adjustment["delta"] = reset_level | minus: inventory_level.quantities.first.quantity %}
        {% assign inventory_adjustment["changeFromQuantity"] = inventory_level.quantities.first.quantity %}
        {% assign inventory_adjustments = inventory_adjustments | push: inventory_adjustment %}
      {% endif %}
    {% endfor %}
  {% endfor %}

  {% comment %}
    -- changeFromQuantity field is required as of 2026-04
    https://shopify.dev/changelog/making-changefromquantity-field-required
    -- Shopify will enforce idempotency for several mutations as of 2026-04
    https://shopify.dev/changelog/making-idempotency-mandatory-for-inventory-adjustments-and-refund-mutations
  {% endcomment %}

  {% if inventory_adjustments != blank %}
    {% assign groups_of_inventory_adjustments = inventory_adjustments | in_groups_of: 250, fill_with: false %}

    {% for group_of_inventory_adjustments in groups_of_inventory_adjustments %}
      {% capture idempotent_key %}
        {
          "task_id": {{ task.id | json }},
          "event_id": {{ event.id | json }},
          "data": {{ group_of_inventory_adjustments | json }}
        }
      {% endcapture %}

      {% action "shopify" %}
        mutation {
          inventoryAdjustQuantities(
            input: {
              reason: "correction"
              name: "available"
              changes: {{ group_of_inventory_adjustments | graphql_arguments }}
            }
          ) @idempotent(key: {{ idempotent_key | json | md5 | json }}) {
            inventoryAdjustmentGroup {
              reason
              changes(quantityNames: "available") {
                name
                delta
                item {
                  id
                  sku
                }
                location {
                  name
                }
              }
            }
            userErrors {
              code
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endif %}
{% endif %}
Mechanic Liquid reference

Default option values

Reset all inventory items to this level
0