Created
April 24, 2025 06:19
-
-
Save matjahs/bc895b6ae5b3e796783d306f7f7fde09 to your computer and use it in GitHub Desktop.
Prune stale data from Unifi Controller
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
/** | |
Usage: | |
1. ssh root@<UDM-Pro IP or hostname> | |
2. wget -O /tmp/mongo_prune.js https://gist.github.com/matjahs/bc895b6ae5b3e796783d306f7f7fde09/raw/30e1a8cf1c63c62e28e690d0dbc730e30b3d75de/mongo_prune.js | |
3. mongo --port 27117 --quiet --eval "var days=14, dryrun=true" /tmp/mongo_prune.js | |
**/ | |
if (!days) { | |
throw new Error('days not defined'); | |
exit(1); | |
} | |
if (typeof dryrun === 'undefined') { | |
throw new Error('dryrun not defined'); | |
exit(1); | |
} | |
var now = new Date().getTime(); | |
var time_criteria = now - days * 86400 * 1000; | |
var time_criteria_in_seconds = time_criteria / 1000; | |
print((dryrun ? "[dryrun] " : "") + "pruning data older than " + days + " days (" + time_criteria + ")... "); | |
db = db.getSiblingDB('ace'); | |
var collectionNames = db.getCollectionNames(); | |
for (var i = 0; i < collectionNames.length; i++) { | |
var name = collectionNames[i]; | |
var query = null; | |
if (name === 'event' || name === 'alarm') { | |
query = { time: { $lt: time_criteria } }; | |
} | |
if (name === 'rogue') { | |
query = { last_seen: { $lt: time_criteria_in_seconds } }; | |
} | |
if (name === 'voucher') { | |
query = { end_time: { $lt: time_criteria_in_seconds } }; | |
} | |
if (name === 'guest') { | |
query = { end: { $lt: time_criteria_in_seconds } }; | |
} | |
if (name === 'user') { | |
query = { | |
blocked: { $ne: true }, | |
use_fixedip: { $ne: true }, | |
noted: { $ne: true }, | |
$or: [ | |
{ last_seen: { $lt: time_criteria_in_seconds } }, | |
{ last_seen: { $exists: false }, first_seen: { $lt: time_criteria_in_seconds } } | |
] | |
}; | |
} | |
if (query) { | |
var count1 = db.getCollection(name).count(); | |
var count2 = db.getCollection(name).find(query).count(); | |
print((dryrun ? "[dryrun] " : "") + "pruning " + count2 + " entries (total " + count1 + ") from " + name + "... "); | |
if (!dryrun) { | |
db.getCollection(name).remove(query); | |
db.runCommand({ compact: name }); | |
} | |
} | |
} | |
if (!dryrun) db.repairDatabase(); | |
db = db.getSiblingDB('ace_stat'); | |
collectionNames = db.getCollectionNames(); | |
for (var i = 0; i < collectionNames.length; i++) { | |
var name = collectionNames[i]; | |
var query = null; | |
if (name.indexOf('stat') === 0) { | |
query = { time: { $lt: time_criteria } }; | |
} | |
if (query) { | |
var count1 = db.getCollection(name).count(); | |
var count2 = db.getCollection(name).find(query).count(); | |
print((dryrun ? "[dryrun] " : "") + "pruning " + count2 + " entries (total " + count1 + ") from " + name + "... "); | |
if (!dryrun) { | |
db.getCollection(name).remove(query); | |
db.runCommand({ compact: name }); | |
} | |
} | |
} | |
if (!dryrun) db.repairDatabase(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment