Skip to content

Instantly share code, notes, and snippets.

@chrisshiplet
Created April 4, 2018 02:25
Show Gist options
  • Save chrisshiplet/669c700b54ed7de0899ada8f86be1cf8 to your computer and use it in GitHub Desktop.
Save chrisshiplet/669c700b54ed7de0899ada8f86be1cf8 to your computer and use it in GitHub Desktop.
Delete all Stripe customers with matching email
#!/usr/bin/env node
// Example usage: TOKEN=sk_test_abcdef node index.js "[email protected]"
if (!process.env.TOKEN) {
throw new Error('Missing TOKEN environment variable');
}
var stripe = require("stripe")(process.env.TOKEN);
if (!process.argv[2]) {
throw new Error('Missing email argument');
}
var ids = [];
(function getPage(startingAfter) {
var opt = { email: process.argv[2], limit: 100 };
if (startingAfter) {
opt.starting_after = startingAfter;
}
stripe.customers.list(opt, (err, customers) => {
if (err) { console.log(err); }
else {
ids.push(...customers.data.map(cus => cus.id));
if (customers.has_more) {
getPage(customers.data[customers.data.length - 1].id);
} else {
console.log(`${ids.length} customers found`);
if (ids.length) {
console.log('deleting...');
(function deleteCustomer() {
var id = ids.pop();
console.log(id);
stripe.customers.del(id, (err, confirm) => {
if (err) { console.log(err); }
else if (ids.length > 0) { deleteCustomer(); }
else {
console.log('done');
}
});
})();
}
}
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment