Delete metafields on app uninstalled

Hi all,

I’m developing an app which create metafields in the Shop scope. I would like to delete these metafields when the app is uninstalled.
I am able to get the list of the metafields in the app/uninstall webhook via this configuration:

In config/initializers/shopify_app.rb:

config.webhooks = [
    { topic: "app/uninstalled", address: "http://mywebsite/webhooks/app_uninstalled", format: "json", "metafield_namespaces": [ "myappnamespace"] },
  ]

Then i’m lopping over my metafields, and try to delete them one by them (as they are no bulk delete yet):

In app/jobs/app_uninstalled_job.rb:

class AppUninstalledJob < ActiveJob::Base
  def perform(shop_domain:, webhook:)
    shop = Shop.find_by(shopify_domain: shop_domain)

    if shop.nil?
      logger.error("#{self.class} failed: cannot find shop with domain '#{shop_domain}'")
      return
    end
    shop.delete()
    webhook[:metafields].each { |meta|
       shop.with_shopify_session do
          ShopifyAPI::Metafield.delete(meta["id"])
       end
    }
  end
end

But I get a 403 error when i’m calling the API to delete them.
I wonder if we still have access to the shop when this webhook is called.

Is there any option to do this ?

Thanks

Hi Lestt,

I also have this problem with my app. I’ve made a post about it: https://community.shopify.com/c/Shopify-APIs-SDKs/App-uninstalled-hook-cannot-use-access-token/m-p/1184664#M67358

This is very unfortunate that Shopify doesn’t seem to allow use of the access token the moment the app is uninstalled. In my case it’s quite an issue because I need to perform a breif cleanup of an edited theme file :confused:

Let me know if you find any updates or solution to this. I hope Shopify can allow the access token to be used briefly after app uninstall.

Is there any option to do this ?

No. The workaround is to conditionally render your snippet content as described here: https://www.shopify.com.au/partners/blog/add-code-from-your-app (see the “Option Two” section).

HTH, Gavin.

@Ari9 Thank you very much for your quick answer, but it does not helped me in my use case.

Hi @Lestt – my names Strat, a FED on the Metafields team :waving_hand:

I’m a little late to the party here. I understand you’d like to remove metafields after an app has been deleted. We don’t have a great solution for this just yet. As you’ve noted, apps no longer have permissions to edit a shop once merchants decide to delete the app. I understand how that can be frustrating when you want to clean up your data. Shopify cannot provide access after merchants have decided to revoke it – we need to respect that decision.

As a result, we don’t provide options for deleting resources after an app has been removed, but you do have some options:

  • As @Gavinator suggested it’s always a good idea to scope your app’s functionality or code to the app with ScriptTags.
  • Similarly, you can use App Extensions to securely surface your app’s functionality on a merchant’s online store. If your app is uninstalled, the section is then removed from themes without further intervention.
  • Outside of a theme, you can scope your app using Private metafields. Private metafields are only accessible to the app that created them, and they’re removed when the app is uninstalled. They’re a great option for creating settings in your app that can disable functionality once apps are deleted.

I hope that’s helpful moving forward!