I have a batch apex class that deletes all the records from a custom object and it is deployed from sandbox to production. Now I have to write an other batch apex to delete all records from other custom object, Can I edit(reuse) the old batch apex class in sandbox and use it for other objects? will it affect the batch class that was deployed in production?

2 Answers 2


When you make an edit of any existing class in a sandbox and redeploy it to Production, the class in production will be overridden with your changes.

This may disturb your functionality. If your batch class is no longer needed in Production, Then you can modify it in the sandbox and re-deploy it to Production with new functionality.

If you don't want to deploy the code to production and just keep it to the sandbox, then you will not have any impact on your production instance.

  • Thank you very much @sanket kumar Commented Jul 12, 2019 at 9:32

Yes, you can do it.

Create a constructor in batch class where you pass the query you want to execute in the start method.

Then in the execute batch method pass the List<sObject> scope instead of the particular object.

On production, however, you should rerun the batch with new constructor.

To summarize, the batch will look somehow like this:

public with sharing class RunFlowBatch
        implements Database.Batchable<sObject> {
    public String query;
    public RunFlowBatch(String query) {
        this.query = query;

    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(this.query);

    public void execute(Database.BatchableContext bc, List<sObject> scope) {
        //do necessary operations

    public void finish(Database.BatchableContext bc) {
        //do necessary operations

  • So, once the code is deployed in production, changes made in sandbox code will not be affected right ? @Mariia IIIarionova Commented Jul 12, 2019 at 9:14
  • If you won't refresh your sandbox or deploy from sandbox to prod - than yes, these are two separated environments. @SalesforceUser Commented Jul 12, 2019 at 9:30
  • Thank you @Maria lllarionova Commented Jul 12, 2019 at 9:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .