Last updated: 2017-07-06

Removing Old Data in MongoDB when Disk is Full

Clearing old data and freeing space back to the OS requires extra space (unless WiredTiger storage engine is in use). This presents a catch-22… to clear space, you need space!

Work-around: Attach a volume and use that as the repair path for MongoDB. We’ll do the following:

Creating the volume

From a client machine

nova volume-create --display-name mongotmp 40
nova volume-attach <VM UUID> <Volume UUID>

Deleting the old data

From within the VM

sudo mkfs.ext4 /dev/vdb # Double-check vdb is what it was mounted as, change as needed
sudo mkdir /var/lib/mongod/tmp # Assumes /var/lib/mongod is the path of the database, change as needed
sudo mount /dev/vdb /var/lib/mongod/tmp

sudo service mongod stop # Do a ps aux and make sure its dead
mongod --dbpath /var/lib/mongod/ --repair --repairpath /var/lib/mongod/tmp
sudo service mongod restart # Do a ps aux and make sure its alive again

df -h # Verify you have free space in your main drive
sudo umount /var/lib/mongod/tmp

Deleting the volume

From a client machine

nova volume-detach <VM UUID> <Volume UUID>
nova volume-delete <Volume name or UUID>