Run cloud functions on Historical data

I have created a cloud function

Moralis.Cloud.afterSave("PublisherUniswap", async function(request) {await addEntryUsageCollection(request); });

 const addEntryUsageCollection = async (request) => {
		let entry;
        
        let addressQuery = new Moralis.Query("Usage");
        addressQuery.equalTo("address", request.object.get("address"));
        
        let toQuery = new Moralis.Query("Usage");
        toQuery.equalTo("to", request.object.get("to"));


        var mainQuery = Moralis.Query.and(addressQuery, toQuery);

        question = await mainQuery.first();

        if (question){
            logger.info("Entry found: "+ question)
        }else{
            entry = new Moralis.Object("Usage");
            entry.set("address", request.object.get("address"));
            entry.set("to", request.object.get("to"));
            entry.save().then(
                (entry) => {
                    logger.info('New Entry Created');
                        }, (error) => {
                        logger.info('Error in inserting new entry');
                    }
            )
    }
  }

It runs successfully whenever a new event is created but doesn’t do anything for the past events.

afterSave is triggered only for new events after it was registered, and it works only for real time sync, it is not run for historical sync

So is there no way to run cloud functions for historical events ?

You can make a separate cloud function that you can run it on all the data. Or you can make a job. You will need a flag to know what was processes already.