Last active
April 23, 2021 06:40
-
-
Save valeriansaliou/857929106a46cef7d478d81ae847b56f to your computer and use it in GitHub Desktop.
Slack archived channels delete
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
{ | |
"dependencies": { | |
"request-promise": "4.2.2", | |
"request": "2.83.0", | |
"promise-seq": "2.0.1" | |
} | |
} |
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
// Notice: SLACK_TOKEN must be a 'xoxs' special token you can get while deleting a channel from the Web app with Chrome Network tools open. A regular token won't do. | |
var token = process.env.SLACK_TOKEN; | |
var request_promise = require("request-promise"); | |
var promise_sequential = require("promise-seq").default; | |
// Tune this to avoid being rate-limited | |
var wait_delete = 10000; | |
// The Slack max. per request is 1000 so no need to change that | |
var list_limit = 1000; | |
console.log("NOTICE: THIS SCRIPT HAS A HARD-CAP OF 1000 CHANNELS PER RUN, RE-LAUNCH AS MANY TIMES AS NEEDED FOR FULL CLEANUP IF YOU HAVE MORE THAN 1000 ARCHIVED CHANNELS TO CLEAN."); | |
function process_chunk(fn_done, fn_error) { | |
var chunk_size = 0; | |
request_promise({ | |
method : "GET", | |
url : "https://slack.com/api/conversations.list", | |
qs : { | |
token : token, | |
limit : list_limit | |
}, | |
json : true | |
}) | |
.then(function(conversations) { | |
if (!conversations || conversations.ok !== true) { | |
return Promise.reject(conversations.error); | |
} | |
chunk_size = conversations.channels.length; | |
console.log("Listed conversations", chunk_size); | |
// Filter on archived channels | |
var channels = conversations.channels.filter(function(channel) { | |
return channel.is_archived === true; | |
}); | |
console.log("Filtered conversations", channels.length); | |
// Sequential removal of match | |
return promise_sequential( | |
channels.map(function(channel, index) { | |
return function() { | |
return Promise.resolve() | |
.then(function() { | |
console.log("Will delete channel", channel.name); | |
return new Promise(function(resolve) { | |
setTimeout(resolve, wait_delete); | |
}); | |
}) | |
.then(function() { | |
return request_promise({ | |
method : "POST", | |
url : "https://slack.com/api/conversations.delete", | |
qs : { | |
token : token, | |
channel : channel.id | |
}, | |
json : true | |
}); | |
}) | |
.then(function(result) { | |
if (!result || result.ok !== true) { | |
return Promise.reject(result.error); | |
} | |
console.log("Deleted channel", channel.name); | |
console.log("Remaining:", channels.length - (index + 1)); | |
return Promise.resolve(); | |
}) | |
.catch(function(error) { | |
console.error("Failed deleting channel", channel.name); | |
return Promise.reject(error); | |
}); | |
}; | |
}) | |
); | |
}) | |
.then(function() { | |
console.info("Chunk done"); | |
fn_done(chunk_size); | |
}) | |
.catch(function(error) { | |
console.error("error", error); | |
fn_error(); | |
}); | |
} | |
function schedule_next_chunk() { | |
process_chunk( | |
function(chunk_size) { | |
if (chunk_size >= list_limit) { | |
console.info("Chunk done, processing next"); | |
schedule_next_chunk(); | |
} else { | |
console.info("Chunk done, no next chunk"); | |
process.exit(0); | |
} | |
}, | |
function() { | |
console.error("Chunk processing aborted because of an error"); | |
process.exit(1); | |
} | |
); | |
} | |
// Schedule chunks processing | |
schedule_next_chunk(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment