Deletion of 1 million records with BulkWrite (DeleteManyModel ) taking longer time

Hi Team,
I have collection of 5 million records and every time data refresh happens , i wanted to delete Approx. 1 Million records. I am using below java code but it taking 30 Sec to delete. that is to high for application response time. Please suggest me better approach to reduce query response time.

I have created compound index on fields which i am using in where clause.

Created compound index on these field : {“collection_id” : 1,“client_id” : 1,“version” : -1}

package com.sample.db.mongo;
import java.util.ArrayList;
import java.util.List;

import org.bson.Document;
import org.json.JSONObject;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.BulkWriteOptions;
import com.mongodb.client.model.DeleteManyModel;
import com.mongodb.client.model.WriteModel;
public class InsertingDocumentBulk {
public static void main( String args[] ) {
//Creating a MongoDB client
MongoClient mongo = new MongoClient( “localhost” , 27017 );
MongoDatabase database = mongo.getDatabase(“integ28”);

long start = System.currentTimeMillis();
    
List<WriteModel<Document>> deleteManyModel = new ArrayList<>();
JSONObject dataVersion = new JSONObject().put("$lt", 23);
JSONObject jsonQuery = new JSONObject();
jsonQuery.put("collection_id", "123321");
jsonQuery.put("version", dataVersion);
jsonQuery.put("client_id", "8");

BulkWriteOptions bulkWriteOptions = new BulkWriteOptions();
bulkWriteOptions.ordered(false);
bulkWriteOptions.bypassDocumentValidation(true);
 
deleteManyModel.add(new DeleteManyModel<Document>(Document.parse(jsonQuery.toString())));
database.getCollection("BulkInsertTesting").bulkWrite(deleteManyModel,bulkWriteOptions); 
   
	System.out.println(System.currentTimeMillis() - start);  

System.out.println("Document Deleted successfully");

}
}

Performances are influenced by the characteristics of your configuration.

So those numbers might be totally appropriate depending of your setup.

What are the characteristics of the machines running mongod? RAM, disk, replica sets, …

Indexes are also an important factor. Deleting documents implies that ALL indexes are updated. If your index {“collection_id” : 1,“client_id” : 1,“version” : -1} exists only for the purpose of your delete then updating the sole purpose index might be the one causing the problem. So removing it might improve your performances.

If removing the index is slower or impossible then you might want to consider adding a field that marks if a document is to be deleted. When the user action causing the delete occurs, you simply mark the documents. This should be fast because no indexes are updated while the user is waiting for a response. You then write a small delete task that distribute over time the physical deletion.