Automating the compact() command via the API

Recently we have cleaned some data from our DB, so we want to reduce storage space.

However, the storage size remains as is. We asked the support team how to Reclaim/reduce disk space in MongoDB and were advised to use the compact command.

We ran the command manually as of now. I was wondering if anyone has a script that utilizes the API to run the command. Here is the advice we got from the support team at MongoDB:

On the topic of automation of any portion of this process, you can decrement the disk size using the Modify A Cluster API endpoint.

Can I ask how you ran it, I am trying to work it out - is this something we can do in Compass or Studio3T?

If you periodically run a command on mongo db, you can create a shell script which does the job and trigger that script with cronjobs. you need to be sure the script should not run multiple times and it will be good to send email the outputs.