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

Mechanic is a development 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 when a user triggers the task and when a bulk operation finishes. Configuration includes reset all inventory items to this level and test mode.

15-day free trial – unlimited tasks

Documentation

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.

Use test mode to see a preview of what this task would do, if test mode were not enabled.

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Gurus, 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.

Open source
View on GitHub to contribute to this task
Events
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
reset all inventory items to this level (number, required), test mode (boolean)
Script
{% if event.topic == "mechanic/user/trigger" %}
  {% capture bulk_operation_query %}
    query {
      inventoryItems {
        edges {
          node {
            id
            __typename
            inventoryLevels {
              edges {
                node {
                  id
                  __typename
                  available
                }
              }
            }
          }
        }
      }
    }
  {% 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 %}
      {"id": "gid://shopify/InventoryItem/1234567890", "__typename": "InventoryItem"}
      {"id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890", "__typename": "InventoryLevel", "available": -5, "__parentId": "gid://shopify/InventoryItem/1234567890"}
    {% endcapture %}

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

  {% for object in bulkOperation.objects %}
    {% if object.__typename != "InventoryLevel" %}
      {% continue %}
    {% endif %}

    {% if event.preview != true and object.available == options.reset_all_inventory_items_to_this_level__number_required %}
      {% continue %}
    {% endif %}

    {% assign adjustment = options.reset_all_inventory_items_to_this_level__number_required | minus: object.available %}

    {% if options.test_mode__boolean %}
      {% log inventoryLevelId: object.id, currentlyAvailable: object.available, adjustment: adjustment %}
    {% else %}
      {% action "shopify" %}
        mutation {
          inventoryAdjustQuantity(
            input: {
              inventoryLevelId: {{ object.id | json }}
              availableDelta: {{ adjustment | json }}
            }
          ) {
            inventoryLevel {
              available
            }
            userErrors {
              field
              message
            }
          }
        }
      {% endaction %}
    {% endif %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.
Defaults
Reset all inventory items to this level
0
Test mode
true