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
tola2278tola2278 

Batch Apex

I need to write a batch  which should delete data from object.If there is any example please share thanks 

sfdcfoxsfdcfox
global with sharing class BatchDelete implements Database.Batchable<SObject> {
    SObjectType entityType;
    Integer limitCount;

    global BatchDelete(SObjectType entityType, Integer limitCount) {
        this.entityType = entityType;
        this.limitCount = limitCount==null?50000000:limitCount;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id FROM '+String.valueOf(entityType)+' LIMIT '+String.valueOf(limitCount));
    }
    global void execute(Database.batchableContext bc, sobject[] scope) {
        database.delete(scope,false);
    }
    global void finish(database.batchablecontext bc) {
    
    }
}

You can adapt this to a specific type of sobject, or change the deletion behavior, possibly even track number of failures and so on. This is just a quick example of how you might get started.