-
-
Save iainelder/81f40e2cd7c884d3bdf100241f9e2543 to your computer and use it in GitHub Desktop.
Script to delete and clear old users from intercom. Useful for lowering the monthly bill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// License: MIT, feel free to use it! | |
const Intercom = require('intercom-client'); | |
const appId = 'APP_ID' | |
const apiKey = 'APP_KEY' | |
const client = new Intercom.Client(appId, apiKey); | |
const async = require('async-q') | |
//REF: https://developers.intercom.com/reference#iterating-over-all-users | |
//WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again | |
//NOTE: list API will only page up to 10k results. It won't list all the users | |
//NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue. | |
const maxAge = 21 // days | |
// Bulk delete option | |
client.users.scroll.each({}, function(d) { | |
const users = d.body.users | |
let deleteUsers = users.filter(user => { | |
let diff = Date.now()/1000 - user.updated_at | |
let age = Math.floor(diff/60/60/24) | |
// Example of custom attributes checking | |
let tier = user.custom_attributes.tier | |
// Delete use if last seen is more than maxAge | |
let deprecate = Boolean(age >= maxAge && (!tier || tier === 'starter')) | |
if (deprecate) console.log('[deleting user]', user.email, age, tier || '') | |
else console.log('[skipped user]', user.email, age, tier || '') | |
return deprecate | |
}).map(user => ({delete: user})) | |
if (deleteUsers.length) { | |
client.users.bulk(deleteUsers).then(resp => { | |
console.log('[deleted users]', deleteUsers.length) | |
// NOTE: if any one operation fails it will return an error but will continue to process the other jobs | |
// ref: https://developers.intercom.com/reference#bulk-apis | |
}).catch(err => console.error('[bulk delet failed]', err)) | |
} | |
}); | |
// --- Example of a async queue based operation --- // | |
// const async = require('async-q') | |
//NOTE: Rate limit is at 500 requests per minute | |
// const delay = 120 //ms | |
// const concurrency = 1 | |
// | |
// const queue = async.queue((user) => { | |
// return client.users.delete(user).then(resp => { | |
// console.log('[deleted user]', user.email, resp.status, | |
// ` ${(new Date(user.updated_at*1000)).toLocaleDateString()}`) | |
// }).then(() => { | |
// // add a delay between each request | |
// return new Promise(resolve => setTimeout(resolve, delay)) | |
// }).catch(err => { | |
// // Ignore errors and continue | |
// // NOTE: remove this catch if you want delete errors to stop the entire script | |
// console.error(err) | |
// }) | |
// }, concurrency) | |
// Using queue and deleting users one by one. | |
// client.users.scroll.each({}, function(d) { | |
// const users = d.body.users | |
// | |
// console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length); | |
// users.forEach(user => { | |
// let diff = Date.now()/1000 - user.updated_at | |
// let age = Math.floor(diff/60/60/24) | |
// // Example of custom attributes checking | |
// let tier = user.custom_attributes.tier | |
// // Delete use if last seen is more than maxAge | |
// if (age >= maxAge && tier != 'professional' && tier != 'print') { | |
// queue.push({id: user.id, updated_at: user.updated_at, email: user.email}) | |
// } else { | |
// if (tier) console.log('[user tier]', user.email, tier) | |
// // console.log('[user age]', age) | |
// } | |
// }) | |
// }); | |
/** | |
Copyright (c) 2010-2016 Thomas Zhou | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment