'How do you cancel a batch of Stripe Subscriptions?
Business went under, what do I do? The Dashboard doesn't let me cancel all subscriptions and schedules in a batch at once with a simple command.
Solution 1:[1]
I had to create what I call the "end of the world" script for when you need to cancel all your customers' subscriptions, or at least a fair number of them.
Steps:
- Go to https://dashboard.stripe.com/subscriptions
- Click Export (and filter according to your needs if you're lucky to keep a few customers)
- Copy and paste the subscription ids into this script or parse the ids from csv file into the variable
- Run it.
- Check https://dashboard.stripe.com/subscriptions again to make sure it's all gone
- Check https://dashboard.stripe.com/subscription_schedules in case you had schedules
- Check https://dashboard.stripe.com/invoices?closed=false&status=open in case you need to stop collection on any invoices
fml.js
const stripe = require('stripe')('your api key goes here');
const subscriptions = [
// paste all subscriptions ids here or parse the array from the first column of the CSV file
];
async function cancelSubscription(s) {
try {
const deleted = await stripe.subscriptions.del(s);
console.log(deleted.id);
} catch (e) {
console.log(e)
}
}
function delay(t, val) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve(val);
}, t);
});
}
(async () => {
for (const sub of subscriptions) {
await cancelSubscription(sub)
await delay(1000) // this delay prevents going over the API limit, you can decrease it
// but you'll miss the opportunity to say goodbye to the ids rolling through your screen
}
})()
Wish you all the best and may you never need to use this piece of code.
Solution 2:[2]
There's no built in way to cancel all subscriptions. But you could do it this way:
- List all subscriptions using this endpoint
- Then cancel each of the subscription using this endpoint
In Node.js it would look something like this:
const subscriptions = await stripe.subscriptions.list();
subscriptions.autoPagingEach(subscription => {
await stripe.subscriptions.del(subscription.id);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marcos |
| Solution 2 | soma |
