Skip to content

Instantly share code, notes, and snippets.

@zthomas
Last active May 1, 2023 15:28
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save zthomas/bb830b7b669507857e0ce38b59c64deb to your computer and use it in GitHub Desktop.
Save zthomas/bb830b7b669507857e0ce38b59c64deb to your computer and use it in GitHub Desktop.
Script to delete and clear old users from intercom. Useful for lowering the monthly bill
// 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.
**/
@stevermeister
Copy link

👍

@sp90
Copy link

sp90 commented Jun 28, 2018

Legit work, for a shite organisational move by intercom 👍

@tomefavre
Copy link

Is it still working @sp90?

@armand-paz
Copy link

Hi!

I am was looking for this sort of solution and am so glad I came across it. Sadly, I am not a dev (marketing guy actually) so was hoping any of you could answer question:

Where do I need to drop this so it can start working? Directly into my site code or elsewhere?

Thanks in advance!

@kumar7rk
Copy link

kumar7rk commented Sep 6, 2018

Hey @armand-paz

I think you should be able to save this on your computer and run like a normal js file.

Make sure to add your intercom APP_KEY, APP_ID (Line-4,5) and download dependencies (Line-3,7)

@kalv
Copy link

kalv commented Oct 30, 2018

For those looking for something that will just do this automatically and even allow you to easily restore users that were archived, we built a service called Hibernately to set something like this up and forget about it.

@rlfrahm
Copy link

rlfrahm commented Dec 6, 2018

@kalv

For those looking for something that will just do this automatically and even allow you to easily restore users that were archived, we built a service called Hibernately to set something like this up and forget about it.

This is pretty neat! Thanks!

@joemellin
Copy link

@kalv I like the idea of it, but I can't have you storing our customer data. If you had a way to auto delete the data without storing it, that may work for us.

@kalv
Copy link

kalv commented Feb 13, 2019

@joemellin Totally makes sense. We've added a delete option on the task we run for you. So we don't store anything if you don't want us to.

@M00NSH00T3R-JPG
Copy link

@zthomas: thanks a lot for this. Instead of running the Scroll API, I've made it segment specific, so that I can archive users segment-wise.
Just one query though, can you please explain what "let tier = user.custom_attributes.tier" means? I'm unable to understand tier and "starter" and probably getting unexpected results.

@xaneem
Copy link

xaneem commented Feb 21, 2019

For those looking for something that will just do this automatically and even allow you to easily restore users that were archived, we built a service called Hibernately to set something like this up and forget about it.

@kalv This is pretty cool and works as advertised! Easier than setting up the script

@Saeven
Copy link

Saeven commented Mar 2, 2020

To get this to work, you'll have to do a few things.

  1. Create an app for your workspace on the Intercom dev hub.
  2. Access the Basic Info section, and set your API version to 1.4
  3. Change the signature to use the token
const client = new Intercom.Client({
  token: "yourtoken"
});

After this, works like magic.

@adamreisnz
Copy link

@kalv is Hibernately still a thing? I can sign up, but it's not providing me any information on how it works, and I don't see the option anywhere to not have you store our customer data before I connect to Intercom.

Also no information on pricing. It says start for free, but when does it stop being free?

Reluctant to sign up because of these reasons (and I imagine many other people as well).

@PaulAlexJackson
Copy link

@kalv I have been using this for a couple years now. This week I now get an error. Is this project dead?

"Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail"

@vartankourshounian
Copy link

vartankourshounian commented May 1, 2023

Hello @kalv ! quick question - currently I have 16K users in Intercom and I want to delete all of the users where there last seen was more than 30 days ago. Can you please let me know step by step how to proceed to get this done? I don't want to manually delete them from the UI obviously.

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