+ Start a Discussion
Ayyagari RameshAyyagari Ramesh 

Batch Apex class to delete the reports which were created 6 months back and is unused

Hi all,

I have written batch class  which will be sheduled to delete reports which were created 6 months and are not used. I am not sure if this is correct or is there anything required please need suggestion for this.
Global class BatchMassDeleteReports Implements Database.batchable<sobject>{
    
     global Database.QueryLocator start(Database.BatchableContext BC){
        
     return Database.getQueryLocator([SELECT Id from Report where CreatedDate <= LAST_N_DAYS:180 AND LastModifiedDate <= LAST_N_DAYS:180]);
     }
     global  void execute(Database.BatchableContext BC,List<Report> scope){
         list<Report> delReports = (list<Report>)scope;
         if(delReports.CreatedDate <= LAST_N_DAYS:180 && delReports.LastModifiedDate <= LAST_N_DAYS:180)
         {
         delete delReports;
         }   
    }
    global void finish(Database.BatchableContext BC){
        
    }

 } 
Best Answer chosen by Ayyagari Ramesh
Ramesh DepaiahRamesh Depaiah
I dont see any other way to automate mass delete, the one above i mentioned looks easy and straight forward way
http://mysalesforcestuff.blogspot.com/2015/06/bulk-deleting-reports-best-way.html

Thanks
Ramesh

All Answers

Ramesh DepaiahRamesh Depaiah
@Ayyagari,
No you doing it wrong you must use LastRunDate or LastViewedDate fields to identify reports which are unused in 6 months
SELECT Id,LastRunDate,LastViewedDate from Report
I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
 
Ayyagari RameshAyyagari Ramesh
Hi Ramesh, 

Please find the modified batch class code below to delete the reports which 6 months old :

global class BatchMassDeleteReports implements Database.Batchable<sObject>{
    

    public Database.QueryLocator start(Database.BatchableContext context)
    {
        return Database.getQueryLocator('Select ID from Report where LastRunDate <= LAST_N_DAYS:180');
    }

    public void execute(Database.BatchableContext context, List<Report> records)
    {
        delete records;
    }
   public void finish(Database.BatchableContext){
        
    }
}  

i am getting the error - Unexpected token ')'. at line 13 column 48

I am not sure where i am going wrong 


Also find the schedule class below :


public DeleteReportSchedulerClass() {
    }
    
    global void execute(SchedulableContext sc){
        try{
            //Executing batch class (size:50).
            BatchMassDeleteReports batchApexSchd = new BatchMassDeleteReports();   
            database.executebatch(batchApexSchd, 50);
            
            }catch(exception ex){
             //Catching any exception that occurs.
            System.Debug('There was an error ' + ex.getMessage());
            }
    }


are there any changes required please do let me know.

Thanks, 
Ramesh
Ramesh DepaiahRamesh Depaiah
Hi Ayyagari,

You can't delete Reports in a bulk. Because Report Object doesn't allow any DML Operations.
It allows only the following operation,
describeSObjects(), query(), retrieve(), search()
There's a inbuild feature where you can mass delete reports 

In Setup, under Data Management > Mass Delete Records, select Mass Delete Reports and configure a filter (for example based on Lastrun date) to find reports that need to be deleted.
Reports that you delete go into the recycle bin. They aren’t permanently deleted until you clear your recycle bin.

Thanks
Ramesh
Ayyagari RameshAyyagari Ramesh
Hi Ramesh, 

Thank you for the valuable information. But will there  be any possible way to automate the deletion of reports without going through the manual 
process ?

Regards, 
Ramesh
Ramesh DepaiahRamesh Depaiah
I dont see any other way to automate mass delete, the one above i mentioned looks easy and straight forward way
http://mysalesforcestuff.blogspot.com/2015/06/bulk-deleting-reports-best-way.html

Thanks
Ramesh
This was selected as the best answer