From time to time you will be faced with the need to clean up the code and, as a part of this process, it can happen that you decide to delete obsolete apex classes. Not a big deal, is it? But what will you do if the apex class is a batch class? The batch class which was not in use for a longer period and now it would be replaced by a new one. What would you do in the ideal case? Right, you would proceed the next steps: check the Apex Jobs, look for your batch class and check if the job is Completed. In case that it is, you would send the batch class to the “Happy hunting ground” where it could process records in batches permanently ad infinitum. By the way, you can review Apex Job log under Setup > Monitoring > Apex Jobs.

Try to imagine, you are going to delete that batch class from your organization environment, but unfortunately, your attempt to delete it will end with the respond:

This apex class is referenced elsewhere in salesforce.com. Remove the usage and try again.” (Fig.1)

You will ask yourself Wait, what? But the Apex Job is Completed!  This error occurs even the status of the batch class in Apex Jobs is Completed. Moreover, when you click on the “Apex Job” link in you will receive the additional error:

“URL does not exist” (Fig.2). Sad, but true. So what would you do?

Workaround/Solution/Resolution

If you are 100% sure that the batch class could be deleted and you will not break some scheduled jobs that are running currently all you need to do is to abort all of the related “pending” jobs by using an Anonymous Apex code. You can execute the code from Eclipse or Developer Console. Here are the steps to success:

  1. Try to delete class – an error will occur (Fig.1)
  2. Click on particular “Apex Job” links i.e. open them in a new tab.
  3. Look at the URL link – you can see the Apex Job ID in the URL (Fig. 3)
  4. Copy and keep the Apex Job Id
  5. Open Execute Anonymous Window (CTRL+E) in developer console or open Execute Anonymous Window in Froce.com IDE
  6. Execute the code below  in order to “Abort Apex Jobs”  – We already know they are already Completed but things happens, we need to Abort them.
  7. Now you are able to delete the batch class