Skip to content

Instantly share code, notes, and snippets.

@bnorton
Last active July 18, 2022 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnorton/f8f3cf5ecf7fc72d0586ad1774ca7802 to your computer and use it in GitHub Desktop.
Save bnorton/f8f3cf5ecf7fc72d0586ad1774ca7802 to your computer and use it in GitHub Desktop.
Delete multiple users from Chameleon

Delete multiple users from Chameleon

Usage: ruby ~/Documents/chameleon-delete-multiple-users.rb ~/Documents/users-to-delete.csv (assuming these are in your Documents folder)

  1. Save chameleon-delete-multiple-users.rb to your Documents folder
  2. Open the file and add your CHAMELEON_API_SECRET and save the file
  3. Use example-users-to-delete.csv or example-email-to-delete.csv for a sample of the CSV format required
  4. Save the users-to-delete.csv to your Documents folder
  5. Open Terminal app and run the "Usage" command above ⬆️
  6. Confirm you want to Run it now with y and hit ENTER.

Please reach out to hello@chameleon.io if you have any questions

api_secret = '<CHAMELEON_API_SECRET>' # Add this and save the file
require 'csv'
require 'json'
return puts('Please add your Chameleon API Secret ⬆️') if /API_SECRET/ === api_secret
return puts("Please provide a CSV filename to open\n `ruby chameleon-delete-multiple-users.rb CSV-FILE-TO-DELETE-FROM.csv") unless ARGV[0]
return puts("The given CSV file was not found") unless File.exists?(ARGV[0])
lines = CSV.parse(File.read(ARGV[0])) # first argument after the script name
header = lines.shift # don't need the header
puts "Do you want to delete #{lines.count} user#{lines.count == 1 ? '' : 's'}? [y/n]"
answer = STDIN.gets.chomp
return puts("Ok, maybe another time") unless /^y(es)?$/ === answer
puts "Starting the deletion of #{lines.count} user#{lines.count == 1 ? '' : 's'}...\n"
lines.each do |line|
url = 'https://api.chameleon.io/v3/edit/profiles/forget'
uid_or_email = line[0] # if the UID or email is in the first column (or pick the 0-indexed column index you want to use)
url += (/@/ === uid_or_email ? "?email=#{uid_or_email}" : "?uid=#{uid_or_email}")
response = `curl -s -X DELETE -H "X-Account-Secret: #{api_secret}" -H "Accept: application/json" #{url}`.chomp
json = JSON.parse(response) rescue nil
puts /deletion/ === response && json ?
"Removed '#{uid_or_email}' from Chameleon with Deletion ID '#{json['deletion']['id']}'" :
"Error removing '#{uid_or_email}' from Chameleon: Raw response: #{response}"
sleep 0.25 # to be nice to the Chameleon API (remove ~4 per second)
puts
end
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
Email
alice@testing.com
bob@testing.com
real@example.com
user@example.com
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
User ID
93
8492
9349
109
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment