Last active
August 29, 2015 14:07
-
-
Save EugeneLiang/c60f504d11a89f79c342 to your computer and use it in GitHub Desktop.
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
// Installation | |
// 1. Install Node.js http://nodejs.org/download/ | |
// 2. In Terminal, cd (navigate) to the directory where you saved this file | |
// 3. Run 'npm install request', let it install, then run 'npm install async'. | |
// 4. Edit the script config below with your token, org, app, and collection name. | |
// 5. To run the script, at the Terminal prompt, run 'node api_baas_deleter.js' | |
// Note: some minor updates were made to this file because.sillypeople | |
// Config | |
var access_token = "{token}"; | |
var as_basepath = "http://api.usergrid.com/{org}/{app}/"; // You need the trailing slash! | |
var collection = "{collection_name}"; | |
// End Config | |
var request = require('request'); | |
var async = require('async'); | |
var authstring = "access_token=" + access_token; | |
var total = 0; | |
var startTime = Date.now(); | |
function deleteRecords() { | |
request.get(as_basepath + collection + "?" + authstring, function(e, r, data) { | |
json = JSON.parse(data); | |
if (json.count === undefined) | |
{ | |
console.log("Error: invalid endpoint. Check your basepath and collection name."); | |
process.exit(1); | |
} | |
console.log("Got " + json.count + " entities"); | |
if (json.count > 0) | |
{ | |
var deletes = []; | |
for (var i = 0; i < json.count; i++) { | |
var deleteFunction = makeDeleteFunction(json.entities[i].uuid); | |
deletes.push(deleteFunction); | |
} | |
async.parallel( | |
deletes, | |
function(err, results) { | |
console.log("Finished batch; re-running"); | |
setTimeout(function() { | |
deleteRecords(); | |
}, 600); // Mandatory, since it seems to not retrieve entities if you make a request in < 600ms | |
return; | |
} | |
); | |
} | |
else | |
{ | |
console.log("No entities, exiting"); | |
var endTime = Date.now(); | |
console.log("Deleted a total of " + total + " entities in " + minutesFromMs(endTime - startTime)); | |
process.exit(0); | |
} | |
}); | |
}; | |
function makeDeleteFunction(uuid) { | |
return function(callback) { | |
console.log("deleted " + uuid); | |
request.del(as_basepath + collection + "/" + uuid + "?" + authstring, function(e, r, data) { | |
total++; | |
callback(r.statusCode); | |
}); | |
}; | |
} | |
function minutesFromMs(time) { | |
return Math.round(((time % 86400000) % 3600000) / 60000).toString() + " minutes"; // minutes | |
} | |
deleteRecords(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment