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

Mechanic is an automation development platform for Shopify. :)

Reset negative inventory levels to zero in bulk

by Isaac Bowen (team@usemechanic.com)

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 when a user triggers the task and when a bulk operation finishes. Configuration includes run daily at midnight.

15-day free trial – unlimited tasks

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.

Events
when a user triggers the task (mechanic/user/trigger)
when a bulk operation finishes (mechanic/shopify/bulk_operation)
Options
run daily at midnight (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 %}
    {% assign bulkOperation = hash %}
    {% assign bulkOperation["objects"] = array %}
    {% assign bulkOperation["objects"][0] = '{"id": "gid://shopify/InventoryItem/1234567890", "__typename": "InventoryItem"}' | parse_json %}
    {% assign bulkOperation["objects"][1] = '{"id": "gid://shopify/InventoryLevel/1234567890?inventory_item_id=1234567890", "__typename": "InventoryLevel", "available": -5, "__parentId": "gid://shopify/InventoryItem/1234567890"}' | parse_json %}
  {% endif %}

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

    {% if object.available >= 0 %}
      {% continue %}
    {% endif %}

    {% action "shopify" %}
      mutation {
        inventoryAdjustQuantity(
          input: {
            inventoryLevelId: {{ object.id | json }}
            availableDelta: {{ object.available | times: -1 | json }}
          }
        ) {
          inventoryLevel {
            available
          }
          userErrors {
            field
            message
          }
        }
      }
    {% endaction %}
  {% endfor %}
{% endif %}
Mechanic tasks are written in Liquid, which makes them easy to write and easy to modify. Learn more about our platform.