Skip to content

Instantly share code, notes, and snippets.

@evangs
Created May 8, 2018 02:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save evangs/85f70573a9046990df4ff820151e0b08 to your computer and use it in GitHub Desktop.
Save evangs/85f70573a9046990df4ff820151e0b08 to your computer and use it in GitHub Desktop.
var https = require('https');
// CONFIGURATION #######################################################################################################
var token = '';
var delay = 300; // delay between delete operations in millisecond
// GLOBALS #############################################################################################################
var baseApiUrl = 'https://slack.com/api/';
var fileListApiUrl= baseApiUrl + 'files.list?token=' + token + '&count=1000';
var deleteApiUrl = baseApiUrl + 'files.delete?token=' + token;
var files = [];
var thirtyDaysAgoTimeStamp = (new Date().getTime() - (1000 * 60 * 60 * 24 * 30)) / 1000;
// ---------------------------------------------------------------------------------------------------------------------
function deleteFile() {
if (files.length == 0) {
return;
}
var fileId = files.shift();
https.get(deleteApiUrl + '&file=' + fileId, function (res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function(){
var response = JSON.parse(body);
if (response.ok === true) {
console.log(fileId + ' deleted!');
} else if (response.ok === false) {
files.push(fileId);
}
setTimeout(deleteFile, delay);
});
}).on('error', function (e) {
console.log("Got an error: ", e);
});
}
// ---------------------------------------------------------------------------------------------------------------------
https.get(fileListApiUrl + '&ts_to=' + thirtyDaysAgoTimeStamp, function(res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(body);
for (var i = 0; i < response.files.length; i++) {
files.push(response.files[i].id);
}
deleteFile();
});
}).on('error', function(e) {
console.log("got an error: ", e);
});
@dominikwilkowski
Copy link

@danibram
Copy link

I just developed a bot that helps you to manage this, any contribution is welcome!
https://github.com/danibram/scrapy-slack-bot

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