Mark a shop record as uninstalled using webhooks
You can use the app/uninstall
webhook to receive notifications when shops uninstall your app and to update your app's shop records accordingly. This tutorial covers a Ruby implementation that subscribes to the webhook and updates the records.
Set up the webhook
Use the add_webhook
generator from the shopify_app gem to set up the app/uninstalled
webhook for the app. The following code adds the app/uninstalled
webhook to your app config and creates the AppUninstalledJob
job class so that you can add uninstallation handler logic.
rails g shopify_app:add_webhook -t app/uninstalled -a {your_app_url}/webhooks/app_uninstalled
Mark the Shop record as uninstalled
To mark the record as uninstalled, you need to update the AppUninstalledJob
job class. In the following example, the app marks the shop as uninstalled by deleting the Shop record:
class AppUninstalledJob < ActiveJob::Base
def perform(args)
shop = Shop.find_by(shopify_domain: args[:shop_domain])
mark_shop_as_uninstalled(shop)
end
private
def mark_shop_as_uninstalled(shop)
shop.uninstall! if shop
end
end
Define a background job
You need to define a background job to ensure that shops with existing installations also have the uninstall webhook set up. In the following example, the RegisterWebhooksForActiveShop
job is defined to iterate all shop records in the database and configure the webhooks.
class RegisterWebhooksForActiveShops < ApplicationJob
queue_as :default
def perform
register_webhooks_for_active_shops
end
private
def register_webhooks_for_active_shops
Shop.find_each do |shop|
ShopifyApp::WebhooksManagerJob.perform_now(
shop_domain: shop.shopify_domain,
shop_token: shop.shopify_token,
webhooks: ShopifyApp.configuration.webhooks
)
end
end
end
Enqueue the background job
Enqueue the RegisterWebhooksForActiveShops
background job to apply the webhook registration. For details on enqueuing ActiveJobs on Rails, refer to the Rails guides.
More tutorials
Manage webhooks with the Admin API: Learn how to create, configure and test webhooks, along with language-agnostic best practices.