Skip to content

Instantly share code, notes, and snippets.

@kennberg
Created May 3, 2016 21:13
Show Gist options
  • Save kennberg/21994873856536cddbfbfac38c73815e to your computer and use it in GitHub Desktop.
Save kennberg/21994873856536cddbfbfac38c73815e to your computer and use it in GitHub Desktop.
Cancel all Stripe subscriptions
var STRIPE_SECRET_KEY = 'YOUR_KEY';
var OBJECTS_PER_REQUEST = 100;
var stripe = require('stripe')(STRIPE_SECRET_KEY);
function processSubs(err, subs) {
if (err) {
console.log(err);
return;
}
if (!subs) {
return;
}
console.log('Subs=' + subs['data'].length);
if (!subs['data'].length) {
return;
}
for (var j = 0; j < subs['data'].length; j++) {
var sub = subs['data'][j];
console.log('Canceling ' + sub['customer'] + ' ' + sub['id']);
stripe.customers.cancelSubscription(sub['customer'], sub['id'], function(err, confirmation) {
if (err) {
console.log(err);
return;
}
console.log(confirmation);
});
}
if (subs['has_more']) {
var sub = subs['data'][subs['data'].length - 1];
subscriptionList(sub['customer'], sub['id']);
}
}
function subscriptionList(customerId, startingAfter) {
var options = { 'customer': customerId, 'limit': OBJECTS_PER_REQUEST };
if (startingAfter) {
options['starting_after'] = startingAfter;
}
stripe.customers.listSubscriptions(options, processSubs);
}
function customerList(startingAfter) {
var options = { 'limit': OBJECTS_PER_REQUEST };
if (startingAfter) {
options['starting_after'] = startingAfter;
}
stripe.customers.list(options, function(err, customers) {
if (err) {
console.log(err);
return;
}
if (!customers) {
return;
}
console.log('Customers=' + customers['data'].length);
if (!customers['data'].length) {
return;
}
for (var i = 0; i < customers['data'].length; i++) {
var customer = customers['data'][i];
processSubs(null, customer['subscriptions']);
}
if (customers['has_more']) {
var customer = customers['data'][customers['data'].length - 1];
customerList(customer['id']);
}
});
}
customerList(null);
@gferrin
Copy link

gferrin commented Mar 19, 2019

this script worked well for me, thanks @kennberg

@matsgrotterud
Copy link

Worked perfect, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment