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.
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
Run daily at midnight
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
{% if options.run_daily_at_midnight__boolean %}
mechanic/scheduler/daily
{% endif %}
mechanic/shopify/bulk_operation
How task subscriptions work
Option types and flags
- Run daily at midnight (boolean)
Task 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_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