Skip to content

Instantly share code, notes, and snippets.

@schappim
Created January 5, 2024 04:02
Show Gist options
  • Save schappim/db01bd88c684bc8ffb97b13561cad685 to your computer and use it in GitHub Desktop.
Save schappim/db01bd88c684bc8ffb97b13561cad685 to your computer and use it in GitHub Desktop.
This is an example of how to delete customers via the Shopify Admin API
require 'httparty'
require 'date'
class ShopifyClient
include HTTParty
base_uri 'https://{shop_domain}.myshopify.com/admin/api/2023-10'
def initialize(api_key, password)
self.class.basic_auth(api_key, password)
end
def delete_usa_customers
customers = get_recent_customers
customers.each do |customer|
if customer['addresses'].any? { |address| address['country_code'] == 'US' }
delete_customer(customer['id'])
end
end
end
private
def get_recent_customers
yesterday = (Date.today - 1).iso8601
response = self.class.get("/customers.json?created_at_min=#{yesterday}", format: :json)
handle_pagination(response)
end
def delete_customer(customer_id)
self.class.delete("/customers/#{customer_id}.json")
end
def handle_pagination(response)
customers = response['customers']
link_header = response.headers['link']
if link_header && link_header.include?('rel="next"')
next_page = link_header.match(/<(.+?)>; rel="next"/)[1]
customers += handle_pagination(self.class.get(next_page, format: :json))
end
customers
end
end
# Usage
shopify_client = ShopifyClient.new('api_key', 'password')
shopify_client.delete_usa_customers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment