Skip to content

Instantly share code, notes, and snippets.

@dfinnema
Created August 24, 2020 09:48
Show Gist options
  • Save dfinnema/065f171e3cd6738bb655ef5303a0e6dd to your computer and use it in GitHub Desktop.
Save dfinnema/065f171e3cd6738bb655ef5303a0e6dd to your computer and use it in GitHub Desktop.
Upload file to server. Then run "mongo --port 27117 < unifi_delete_vouchers.js"
/*
* This will delete all Unifi vouchers which creation date is older then the days set.
*
* WARNING: applies to all sites
*
* Tested on Version 5.13.x
*/
// keep N-day worth of data
var days=90;
// change to false to have the script to really exclude old records
// from the database. While true, no change at all will be made to the DB
var dryrun=true;
// Grab the Date and calculate the days
const now = Math.round(Date.now() / 1000);
var time_criteria = (now - ( days * 86400 ));
// Access the Unifi DB
use ace;
// Grab all the Voucher Created Times
var voucherCreatedTimes = db.voucher.distinct("create_time");
// Loop through each one
for (i=0; i<voucherCreatedTimes.length; i++) {
var Epoch = voucherCreatedTimes[i];
var Epoch = Epoch.toString().replace("NumberLong(","");
var Epoch = Epoch.toString().replace(")","");
var Epoch = parseInt(Epoch);
// Check if older then x days
if (Epoch < time_criteria) {
print((dryrun ? "[dryrun] " : "") + "removing voucher batch with start time of: " + Epoch);
if (!dryrun) {
// Remove the voucher batches created during this time
db.voucher.remove({"create_time": NumberLong(Epoch)})
}
}
}
// Quick Repair, just in case
if (!dryrun) db.repairDatabase();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment