Reset negative inventory levels to zero in bulk, with Mechanic.

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

Reset negative inventory levels to zero in bulk

Super simple. Scans all inventory levels in your store, on demand, and brings the negative ones back up to zero. Optionally, configure this task to run nightly.

Runs Occurs when a user manually triggers the task and Occurs when a bulk operation is completed. Configuration includes run daily at midnight.

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
{% if options.run_daily_at_midnight__boolean %}
  mechanic/scheduler/daily
{% endif %}
mechanic/shopify/bulk_operation
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
run daily at midnight (boolean)
Code
{% if event.topic == "mechanic/user/trigger" or event.topic == "mechanic/scheduler/daily" %}
  {% 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":-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 < 0 %}
        {% assign inventory_adjustment = hash %}
        {% assign inventory_adjustment["inventoryItemId"] = inventory_item.id %}
        {% assign inventory_adjustment["locationId"] = inventory_level.location.id %}
        {% assign inventory_adjustment["delta"] = inventory_level.quantities.first.quantity | times: -1 %}
        {% 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