Cannot find a metafield definition

I’m working on an app that uses metafields to store extra data on orders, draft orders, products etc. My problem is this: When I run the mutation metafieldDefinitiionCreate with the namespace $app:order, key requestedDeliveryDate and ownerType ORDER, I get a response that says “Namespace and key is already in use for a set of your metafields.”.

I’ve hunted this unknown metafield down and cannot find it. If I run a query for metafieldDefinitions with the same ownerType, nothing shows up. In fact, I’ve run it with every ownerType just to see if the metafield defintion was added to the wrong type. I’ve used the admin UI to look for it, I’ve tried different api versions and I cannot find it. My only other thought is that it was added to a resource without an explicit metafield definition (is that possible?). If so, how would I find it? Is there a way to delete all app metafields and start over?

Since posting this, I’ve discovered that the metafield does exist and is being used on individual resources without a definition. So now the remaining question is: How can I add a definition for a metafield that’s already in use? Is the only way to remove the metafield in use to iterate through all the resources and remove it?

You may be able to migrate an existing metafield to a definition using the following guide: https://help.shopify.com/en/manual/custom-data/metafields/metafield-definitions/migrating-metafields-to-a-definition. However, note that not all types are supported for migration.

1 Like

After some manual testing. I iterated through all the orders, found every instance of one with that metafield on it, deleted it manually, and after that, was able to add the metafield definition as expected.

This could be automated with code to make it a lot easier, but I only had about 50 orders to go though, so it wasn’t worth the time.

Hey @tobiasatportul ,

Happy to see you found answers here in the thread, and have an idea of what you are looking to do moving forward. Just wanted to follow up with another resource, specifically pointing to the wealth of guides covering custom data (metafields and metaobjects); two of which are directly related to your post here. One covers standard definitions, and another focused on managing (creating, updating and deleting) metafields definitions - more here.

Cheers,
Shopify Developer Support