3 Steps to prune Mongo DB in Unifi Controller

By snorlaxprime

You will need two session of terminal to repair, the first session to run the mongo daemon and the second session to prune the database. You will need the mongo_prune_js.js.

Step 1. Login to the first session of the Unifi Controller

Once you login successfully, Stop the unifi service using the following command

service unifi stop

make sure there is no Mongodb session using the following command

ps -aux | grep mongo

Run the mongodb using the following command

sudo mongod --port 27117 -dbpath /usr/lib/unifi/data/db -smallfiles -logpath /usr/lib/unifi/logs/server.log -journal

you should see something similar to the following window:

start mongo db

Step 2. Login to the second session of the Unifi Controller

Download the pruning script, if you don’t have it already. Edit the file using the following command

vi mongo_prune_js.js

and make sure the following line is there

var days=0;
var dryrun=true;

Then execute the pruning script as below:

sudo /usr/bin/mongo --port 27117 < mongo_prune_js.js

You should see something like the following when it finished pruning

Mongo db pruning

Once it is success you need to run it again but this time edit the mongo_prune_js.js with the following

var days=0;
var dryrun=false;

When it is running completely you should see something similar to the following:

Step 3. run the unifi service

Jump back to the first session, and stop the running mongo db using Ctrl + C. You should be back to your command prompt now.

Start the unify server using the following:

service unifi start

If all goes well you should see the following message

Unifi start service

Double check the log files to ensure that the unifi service started successfully using the following command

tail -f /usr/lib/unifi/logs/mongod.log
tail -f /usr/lib/unifi/logs/server.log

You can also check that your Unifi web interface have started successfully using the following


If you encounter problem pruning because you are running out of space, you can find the files that are big using the following command

find / -type f -size +20M -exec ls -lh {} \; | awk '{ print $NF ": " $5 }'

I hope this post have saved you time and frustration.