Reset all inventory levels to a single level, in bulk, with Mechanic.

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

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.

Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes reset all inventory items to this level.

15-day free trial – unlimited tasks

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
reset all inventory items to this level (number, required)
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_adjustments = inventory_adjustments | push: inventory_adjustment %}
      {% endif %}
    {% endfor %}
  {% endfor %}

  {% 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 %}
      {% action "shopify" %}
        mutation {
          inventoryAdjustQuantities(
            input: {
              reason: "correction"
              name: "available"
              changes: {{ group_of_inventory_adjustments | graphql_arguments }}
            }
          ) {
            inventoryAdjustmentGroup {
              reason
              changes {
                name
                delta
                quantityAfterChange
                item {
                  id
                  sku
                }
                location {
                  name
                }
              }
            }
            userErrors {
              code
              field
              message
            }
          }
        }
      {% endaction %}
    {% endfor %}
  {% endif %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Reset all inventory items to this level
0