function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SaranrajSaranraj 

How to select and delete apex class in VF page

Hi all,
        I write the code for view the list of apex class name in VF page and select and delete the apex class name. But i got the error
Compile Error: DML not allowed on ApexClass at line 12 column 6.  how to fix to solve this error and how to delete the the apex class name in VF page Please help  below is my code...

public class RetrieveClasses{
  
  public void del(){
    List <ApexClass> li=new List <ApexClass>();
     for(sObjectWrapper wrap:wrappers)    
     {
      if(wrap.isSelected){
        li.add(wrap.apx);
      }
     }
     system.debug('dfddddd'+li);
     delete li;
     //li[0].status='Inactive';
    // update li;
    
    }
    
  public void triggerdel(){
   List <ApexTrigger> trggerlist=new List <ApexTrigger>();
   for(sObjectWrapper sobj:wrapp)
   {
      if(sobj.isSelected){
       trggerlist.add(sobj.apt);
       }
   }
   //delete trggerlist;
  }

  public List<sObjectWrapper> wrappers{get;set;}
  public List<sObjectWrapper> wrapp{get;set;}

public RetrieveClasses()
{
  wrappers=getdata();
  wrapp=gettrigger();
}
private List<sObjectWrapper> getdata(){
wrappers = new List<sObjectWrapper>();
for(ApexClass app:[select id,name,apiversion from ApexClass where apiversion=36.0])
{
   wrappers.add(new sObjectWrapper(app,false));
}
system.debug('deleteeeeeeeee===='+wrappers);
return wrappers;
}

private List<sObjectWrapper> gettrigger(){
List<sObjectWrapper> wrapper = new List<sObjectWrapper>();
for(ApexTrigger aptg:[select id,name from ApexTrigger])
{
wrapper.add(new sObjectWrapper(aptg,false));
}
system.debug('trigger values'+wrapper);

return wrapper; 
}
//public boolean isSelected{get;set;}

    public class sObjectWrapper{
    public boolean isSelected{get;set;}   
    public ApexClass apx{get;set;}
    public ApexTrigger apt{get;set;}

    
    public sObjectWrapper(ApexClass apx,Boolean isSelected){
     this.isSelected = isSelected;
     this.apx = apx;
    }
    public sObjectWrapper(ApexTrigger apt,Boolean isSelected){
     this.isSelected = isSelected;
     this.apt= apt;
    }
    
}
}
Abhishek_DEOAbhishek_DEO

You cannot delete apexclass like this. Please go through this doc (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_apexclass.htm) that says

 Although Apex classes and triggers have the Create and Update field properties, a runtime exception occurs if you try to create or update them using the API.

However, nothing is mentiond about delete() call but I believe same is true for Delete() call too. You need to use Force.com IDE or the Ant Migration tool. Although, there are other who have used metdata API in APEX. You may check this (https://andyinthecloud.com/2013/10/27/introduction-to-calling-the-metadata-api-from-apex/

SaranrajSaranraj
Hi Abhishek, 
             Thanks for your response,  i refere above the link, can i able to delete the apex class without Force.com IDE  and eclips?
i will delete the apex class on production please give any idea..