Activity › Forums › Salesforce® Discussions › How can we hard delete a record using a Apex class/by code in salesforce?
Tagged: Delete Record, Salesforce Apex, Salesforce Apex Class, Salesforce Apex Code, Salesforce Records, Salesforce SOQL
-
How can we hard delete a record using a Apex class/by code in salesforce?
Posted by Saurabh on May 12, 2017 at 1:40 PMHow can we hard delete a record using a Apex class/by code in salesforce?
Parul replied 7 years, 7 months ago 4 Members · 3 Replies -
3 Replies
-
Hi saurabh,
We can hard delete record or list of records using emptyRecycleBin() function in apex. Pass the record or record list to emptyRecycleBin() to delete it from Recycle Bin.
Contact con = new Contact(Id = ’09k110000O5abc’);
Database.emptyRecycleBin(con);Note: The DML operation datatbase.emptyRecycleBin() is limited to 200 items.
Thanks.
- [adinserter block='9']
-
Hi,
Hard delete is done by using DataBase.emptyRecycleBin method in the Batch class. Create a sample Batch class as mentioned below and use DataBase.emptyRecycleBin method in the Batch class.
global class BatchDeletion implements Database.Batchable<sObject>, Schedulable
{
global BatchDeletion()
{
//constuctor
}global Database.QueryLocator start(Database.BatchableContext bc)
{
//query to return all expired Case Share records
return Database.getQueryLocator([Select id from Account where Name=’Test Account12′]);
}global void execute(SchedulableContext sc)
{
//execute the batch
BatchDeletion deleteCS = new BatchDeletion();
ID batchprocessid = Database.executeBatch(deleteCS);
}global void execute(Database.BatchableContext BC, list<sObject> scope)
{
System.debug(‘## deleting ‘+scope.size()+’ case share recs’);//delete list of expired Case Share records
delete scope;
Database.emptyRecycleBin(scope);
}global void finish(Database.BatchableContext BC)
{
//no post processing
/* System.debug(‘## Batch Job Finished ##’);
UpdateAccountFields m = new UpdateAccountFields ();
String sch = ’20 30 8 10 2 ?’;
system.schedule(‘Merge Job’, sch, m);*/}
}Hope this helps.
-
Hi
Apex:
1. Delete your record using Standard DML for delete:
Database.DeleteResult[] delete(SObject[] recordsToDelete, Boolean opt_allOrNone)2. Empty recycle bin for the records you have just deleted using :
Database.EmptyRecycleBinResult[] emptyRecycleBin(ID [] recordIds)API
1. Delete your record using Standard DML for delete:
DeleteResult[] = connection.delete(ID[] ids);2. Empty recycle bin for the records you have just deleted using :
EmptyRecycleBinResult[] = connection.emptyRecycleBin(ID[] ids);Thanks
Log In to reply.