Make products unavailable, after the date/time stored in product metafields
Use this task to automatically set product inventory to 0, and the product's inventory policy to deny out-of-stock purchases, after a date/time that you specify in a product metafield.
Try this taskWhen it runs
- Occurs when a user manually triggers the task
- Occurs when a bulk operation is completed
What you choose
- Product metafield
- Run hourly
- Run daily
Documentation
Use this task to automatically set product inventory to 0, and the product's inventory policy to deny out-of-stock purchases, after a date/time that you specify in a product metafield.
Configure the "Product metafield" field with the namespace and key (e.g. "custom.unpublish_after") for the product metafield that holds a Shopify date or date_time value. More information on setting up Shopify metafields can be found here.
When you run this task (or as it runs hourly/daily, per your configuration), the task will look for products who have a metafield date/time value that's in the past. For qualifying products, all inventory items with a level greater than 0 will have their levels set to exactly 0, and all variants will have their inventory policies set to "deny" (preventing sales, once the variants are all out of stock).
Note: If you choose to use the date only type for the metafield, then the task will receive those dates as midnight local shop time. So, if this task encounters a metafield date that is the same as the task run date, then that metafield date will be in the past based on a seconds-since-midnight calculation.
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_hourly__boolean %}
mechanic/scheduler/hourly
{% elsif options.run_daily__boolean %}
mechanic/scheduler/daily
{% endif %}
mechanic/shopify/bulk_operation
How task subscriptions work
Option types and flags
- Product metafield (required)
- Run hourly (boolean)
- Run daily (boolean)
Task code
{% if event.topic == "mechanic/user/trigger" or event.topic contains "mechanic/scheduler/" %}
{% capture bulk_operation_query %}
query {
products {
edges {
node {
__typename
id
metafield(key: {{ options.product_metafield__required | json }}) {
value
}
variants {
edges {
node {
__typename
id
inventoryPolicy
inventoryItem {
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" %}
{% assign now_s = "now" | date: "%s" | times: 1 %}
{% if event.preview %}
{% capture bulkOperation_objects_jsonl %}
{"__typename":"Product","id":"gid:\/\/shopify\/Product\/1234567890","metafield":{"value":"2023-04-01"}}
{"__typename":"ProductVariant","id":"gid:\/\/shopify\/ProductVariant\/1234567890","inventoryPolicy": "CONTINUE","inventoryItem":{"id":"gid:\/\/shopify\/InventoryItem\/1234567890"},"__parentId":"gid:\/\/shopify\/Product\/1234567890"}
{"__typename":"InventoryLevel","id": "gid:\/\/shopify\/InventoryLevel\/1234567890?inventory_item_id=1234567890","location":{"id":"gid:\/\/shopify\/Location\/1234567890"},"quantities":[{"name":"available","quantity":4}],"__parentId":"gid:\/\/shopify\/ProductVariant\/1234567890"}
{% endcapture %}
{% assign bulkOperation = hash %}
{% assign bulkOperation["objects"] = bulkOperation_objects_jsonl | parse_jsonl %}
{% endif %}
{% assign bulk_products = bulkOperation.objects | where: "__typename", "Product" %}
{% assign bulk_variants = bulkOperation.objects | where: "__typename", "ProductVariant" %}
{% assign bulk_inventory_levels = bulkOperation.objects | where: "__typename", "InventoryLevel" %}
{% for product in bulk_products %}
{% assign summary = array %}
{% if product.metafield.value == blank %}
{% continue %}
{% endif %}
{% assign product_date = product.metafield.value | parse_date: "%Y-%m-%d" %}
{% if product_date == nil %}
{% log
message: "Found a product metafield value that doesn't match the Shopify date or date_time metafield format. Skipping.",
product_metafield_value: product.metafield.value
%}
{% continue %}
{% endif %}
{% assign product_date_s = product.metafield.value | date: "%s" | times: 1 %}
{% if product_date_s > now_s %}
{% continue %}
{% endif %}
{% assign variants = bulk_variants | where: "__parentId", product.id %}
{% assign variant_ids_to_update = array %}
{% assign inventory_adjustments = array %}
{% for variant in variants %}
{% assign inventory_levels = bulk_inventory_levels | where: "__parentId", variant.id %}
{% if variant.inventoryPolicy != "DENY" %}
{% assign variant_ids_to_update = variant_ids_to_update | push: variant.id %}
{% assign summary[summary.size]
= "Change variant inventory policy for "
| append: variant.id
| append: " to DENY"
%}
{% endif %}
{% for inventory_level in inventory_levels %}
{% if inventory_level.quantities.first.quantity > 0 %}
{% assign inventory_adjustment = hash %}
{% assign inventory_adjustment["inventoryItemId"] = variant.inventoryItem.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 %}
{% assign summary[summary.size]
= "Set available inventory for "
| append: variant.id
| append: " at location "
| append: inventory_level.location.id
| append: " to 0, by adjusting it by -"
| append: inventory_level.quantities.first.quantity
%}
{% endif %}
{% endfor %}
{% endfor %}
{% if summary != blank %}
{% log product_id: product.id, updates_to_be_made: summary %}
{% endif %}
{% 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 %}
{% if variant_ids_to_update != blank %}
{% action "shopify" %}
mutation {
productVariantsBulkUpdate(
allowPartialUpdates: true
productId: {{ product.id | json }}
variants: [
{% for variant_id in variant_ids_to_update -%}
{
id: {{ variant_id | json }}
inventoryPolicy: DENY
}
{%- endfor %}
]
) {
product {
id
title
}
productVariants {
displayName
sku
inventoryPolicy
}
userErrors {
field
message
}
}
}
{% endaction %}
{% endif %}
{% endfor %}
{% endif %}
Mechanic Liquid reference