Created
January 23, 2023 23:41
-
-
Save alalonde/80cfd1e775a0ccf7f8aa2824d0e0f227 to your computer and use it in GitHub Desktop.
Delete all channels in stream.io chat
This file contains 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
import { StreamChat } from 'stream-chat'; | |
const serverClient = StreamChat.getInstance( | |
'<api key>', | |
'<api secret>', | |
); | |
(async () => { | |
while (true) { | |
let channelIds = []; | |
while (channelIds.length < 100) { | |
const limit = channelIds.length > 70 ? 100 - channelIds.length : 30; | |
const channels = await serverClient.queryChannels({}, {}, {limit}); | |
if (channels.length === 0) { | |
console.log('No channels left!'); | |
process.exit(0); | |
} | |
channelIds = channelIds.concat(channels.map(ch => ch.cid)); | |
} | |
try { | |
await serverClient.deleteChannels(channelIds, {hard_delete: true}); | |
} catch (e) { | |
if (e.response.status === 429) { | |
const limitReset = parseInt(e.response.headers['x-ratelimit-reset'], 10); | |
const waitFor = limitReset - (Date.now() / 1000.0); | |
console.log(`Rate limited, waiting ${waitFor} seconds...`); | |
await new Promise((resolve) => { | |
setTimeout(resolve, waitFor * 1000); | |
}); | |
} else { | |
throw e; | |
} | |
} | |
console.log(`Deleting ${channelIds.length} channels`); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment