Record Cleanup Script
The following script is used to delete records older than the specified time from theapinizer_log collection stored in MongoDB. This operation optimizes storage space by cleaning the database.
How It Works
1
Defining MongoDB Connection Information
Information required to connect to MongoDB server is defined in relevant variables: HOST, PORT, DB_NAME, USERNAME, PASSWORD, and AUTH_DB.
2
Determining Time Period
The TIME_VALUE variable specifies how many hours ago records will be deleted. This value can be in hours (H) or days (D).
3
Preparing MongoDB Commands
MongoDB commands are defined in the MONGO_COMMANDS variable to find and delete documents older than the specified time.
4
Executing Commands
Connect to MongoDB server using the mongosh command and execute the commands defined in the MONGO_COMMANDS variable.
5
Checking Results
When the operation is completed, the message “The command has been executed. Please check the content of the related collection.” is printed to the screen.
Usage
Before running the script, enter your own information in the MongoDB variables. You can update the TIME_VALUE variable according to your desired hour/day value. For example:- To keep documents from the last 3 hours and delete the rest:
TIME_VALUE=3H - To keep documents from the last 5 days and delete the rest:
TIME_VALUE=5D
- Copy the script to a file using a text editor (vi, nano, etc.) and edit the variables inside
- Give execute permission to the file:
chmod +x purge_apinizer_log_collection.sh - Run the file:
./purge_apinizer_log_collection.sh
& character at the end of the command:
CronJob Usage
1
Opening Cron Editor
Open the cron editor by running the following command in the terminal:
2
Adding Cron Job
Add a line to the opened editor according to how often you want to run the script.For example, to run it every day at 23:59:
3
Saving
Press Esc and type :wq and press Enter to save the line you added.
Data cleanup operation will be performed according to the time period you specified in the script. Therefore, it is important to ensure that the time period in the script and the trigger time you set with cron are consistent.

