Skip to content

Instantly share code, notes, and snippets.

@jkentjnr
Last active August 29, 2015 14:26
Show Gist options
  • Save jkentjnr/9241354c38365636029d to your computer and use it in GitHub Desktop.
Save jkentjnr/9241354c38365636029d to your computer and use it in GitHub Desktop.
Blog - USEFUL APEX DELETE BATCH
global class Utility_Batch_Delete implements Database.Batchable<SObject>
{
private String q;
private String fq;
private String[] qs;
global Utility_Batch_Delete(String query)
{
q = query;
}
global Utility_Batch_Delete(String query, String finalQuery)
{
q = query;
fq = finalQuery;
}
global Utility_Batch_Delete(String[] queryList, String finalQuery)
{
q = queryList.get(0);
queryList.remove(0);
qs = queryList;
fq = finalQuery;
}
global Database.QueryLocator start(Database.BatchableContext info)
{
return Database.getQueryLocator(q);
}
global void execute(Database.BatchableContext BC, List<Sobject> scope)
{
delete scope;
}
global void finish(Database.BatchableContext BC)
{
if (qs != null && qs.size() > 0)
{
Utility_Batch_Delete proc = new Utility_Batch_Delete(qs, fq);
Database.executeBatch(proc);
}
else
{
if (fq != null)
{
List<SObject> lst = Database.query(fq);
delete lst;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment