Clearing Metafields Issue: "Value can't be blank" Error Despite Explicitly Setting Value to ""

Clearing Metafields Issue: "Value can't be blank" Error Despite Explicitly Setting Value to ""

prezzicrazy
Visitor
1 0 0

Hi everyone,

I'm experiencing an issue when attempting to clear (delete) metafields using the Shopify GraphQL Admin API (version 2025-04). My goal is to remove the metafield so that after the import the metafield does not exist for that product. According to Shopify’s guidelines, if a metafield’s value is empty, it should be deleted (not just set to an empty string).

I've implemented the following approach in Python using the requests library to perform the GraphQL call (along with watchdog to monitor local file changes). For metafields that should be cleared, I explicitly set the value field to an empty string ("") and execute a mutation designed to clear them:

if clear_fields:
    query_clear = """
    mutation ClearMetafields($metafields: [MetafieldsSetInput!]!) {
      metafieldsSet(metafields: $metafields) {
        metafields {
          key
          namespace
          value
          updatedAt
        }
        userErrors {
          field
          message
        }
      }
    }
    """

My payload for the metafields I want to clear looks like this:

{
  "metafields": [
    {
      "ownerId": "gid://shopify/Product/9722952974604",
      "namespace": "custom",
      "key": "data_spedito_da",
      "value": "",
      "type": "single_line_text_field"
    },
    {
      "ownerId": "gid://shopify/Product/9722952974604",
      "namespace": "custom",
      "key": "data_venduto_da",
      "value": "",
      "type": "single_line_text_field"
    }
    // ... additional fields ...
  ]
}

Despite this, the API always returns an error like:

[{'field': ['metafields', '0', 'value'], 'message': "Value can't be blank."}, ...]

I have verified that the payload is sent exactly as shown (the logging output confirms that the value field is present and set to ""), yet the error persists.

Details:

API Version: 2025-04

Libraries Used: Python requests library for HTTP calls, and watchdog for file monitoring.

Expected Behavior: If a metafield's value is empty in the CSV import, then that metafield should be deleted. The mutation should delete the metafield (or at least result in its absence), not leave an empty value.

Issue: Instead of deleting the metafield, the API responds with "Value can't be blank."

Question:

How can I achieve this? Is there a recommended approach or a required payload modification that allows me to update a metafield so that its value becomes empty (cleared) without deleting the metafield entry entirely? Essentially, how do I pass a value that renders the metafield empty—so that my theme automatically hides empty values—without triggering the "Value can't be blank" error?

Any insight or guidance would be greatly appreciated. Thanks in advance!

Replies 0 (0)