Reading list Switch to dark mode

    Mass Delete In Lightning Component

    In this blog we will learn how to delete selected (mass delete) record on button click in Salesforce Lightning Component.


    Steps to add the custom button and perform Mass Delete of selected record.

    Step 1:   Create a lightning component and add following code.

    <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" controller="accListClass" >
        <aura:attribute name="account" type="List"></aura:attribute>
        <aura:handler name="init" value="{!this}" action="{!c.accountList}"></aura:handler>
        <div class="slds-grid">
            <div class="slds-col slds-p-horizontal_small slds-size_1-of-2 slds-medium-size_5-of-6 slds-large-size_8-of-12"></div>
            <div class="slds-col slds-size_1-of-8 allBtn"><lightning:button label="Delete Selected"
        <table class="slds-table slds-table--bordered slds-table--striped slds-table--cell-buffer slds-table--fixed-layout">
            	<tr class="slds-text-heading--label">
                    <th scope="row" style="width: 50px;" class="slds-text-align--right"><ui:inputCheckbox aura:id="cbox" class="cBox" change="{!c.selectAll}"/></th>
                    <th scope="row"><div class="slds-truncate" title="ID">ID</div></th>
                    <th scope="row"><div class="slds-truncate" titile="NAME">Name</div></th>
                    <th scope="row"><div class="slds-truncate" title="INDUSTRY">Industry</div></th>
                    <th scope="row"><div class="slds-truncate" title="PHONE">Phone</div></th>
                <aura:iteration items="{!v.account}" var="a">
                        <td><ui:inputCheckbox aura:id="cboxRow" text="{!a.Id}" class="cBox" change="{!c.changeSelectAll}"/></td>
                    	<th><div class="slds-truncate">{!a.Id}</div></th>
                        <td><div class="slds-truncate">{!a.Name}</div></td>
                        <td><div class="slds-truncate">{!a.Industry}</div></td>
                        <td><div class="slds-truncate">{!a.Phone}</div></td>

    Step 2: Create the client side controller of above mentioned Component and add the following code.

        accountList : function(component, event, helper) {
        selectAll: function(component,event, helper){
            var slctCheck = event.getSource().get("v.value");
            var getCheckAllId = component.find("cboxRow");
            if (slctCheck == true) {
                for (var i = 0; i < getCheckAllId.length; i++) {
                    component.find("cboxRow")[i].set("v.value", true);             
            } else {
                for (var i = 0; i < getCheckAllId.length; i++) {
                    component.find("cboxRow")[i].set("v.value", false);
        changeSelectAll:function(component,event, helper){
            var slctCheckRow = event.getSource().get("v.value");
            var getCheckAllId = component.find("cbox");
            if(slctCheckRow == false) {
                component.find("cbox").set("v.value", false);
        deleteSlctd : function(component,event,helper) {
            var getCheckAllId = component.find("cboxRow");
            var selctedRec = [];
            for (var i = 0; i < getCheckAllId.length; i++) {
                if(getCheckAllId[i].get("v.value") == true )

    Step 3: Now create the helper controller of above mentioned Component and add the following code.

    Searching for an experienced
    Salesforce Company ?
    Read More
        getAccountList : function(component) {
            var action = component.get("c.getAccounts");
            var self = this;
            action.setCallback(this, function(actionResult){
        deleteSelected : function(component,event,selctedRec){
            var action = component.get("c.delSlctRec");
                "slctRec": selctedRec
            action.setCallback(this, function(response){
                var state =  response.getState();
                if(state == "SUCCESS")
                    console.log("Successfully Deleted..");
                }else if (state=="ERROR") {

    Step 4: Last step create the apex class name accListClass and add the following code.

    public class accListClass{
        public List<Account> accList = new List<Account>();
        public List<Account> delRec = new List<Account>();
        public static List<Account> getAccounts(String query)
            accListClass alc = new accListClass();
            alc.accList = Database.query('SELECT Id,Name,Phone,Industry from Account');
            return alc.accList;
        public static List<Account> delSlctRec(List<String> slctRec)
            accListClass alc = new accListClass();
            alc.delRec = [SELECT Id FROM Account WHERE Id IN: slctRec ];
               delete alc.delRec;
               } catch(Exception ex)
                 throw new AuraHandledException(ex.getMessage());
            alc.accList = Database.query('SELECT Id,Name,Phone,Industry from Account');
            return alc.accList;



    Select all the record. To implement the pagination in lightning component Click Here

    Click on the button to delete records:


    That’s all for implementation of mass delete in lightning component, still if you have any further query or seek assistance to make your salesforce classic apps compatible with lightning experience, feel free to add a ticket, we will be happy to help you .


    . . .
    Add a comment

    Leave a Comment

    Your email address will not be published. Required fields are marked*


  • shahwaz
     what is accListJsClass ??
    • Gautam Bagchi (Moderator)

      “Sorry for the inconvenience caused! Please replace all the occurrences of accListJsClass to accListClass. It was a typo mistake, we have updated the same on the blog.

  • Back to Top
    Great solution and the techs are very knowledgeable and helpful. Especially Nishi and Abhishek.
    Ann Beattie
    Senior Salesforce Consultant
    Talk to Sales


    Live Chat

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content