Salesforce makes it easy to work with records in bulk using List Views, but when it comes to deleting multiple records directly from a List View — there’s no native “Delete Selected” button. That’s where this solution comes in!
In this blog post, we’ll show you how to create a custom List View button that lets users select multiple records and delete them with a single click using Visualforce, Apex, and StandardSetController.
🎯 Use Case
Let’s say you manage a custom object like Stocks__c and you want users to delete multiple records at once from a List View — just like a mass delete action. We’ll build that using a Visualforce page and an Apex extension.
🧱 Step 1: Apex Class to Handle Deletion
This Apex class is designed to process the selected records from the list view and perform the delete operation.
public class DeleteRecordExtension {
List<sObject> sObjList;
public boolean isDeleted { get; set; }
Set<Id> recordIds;
public String errorMessage { get; set; }
public DeleteRecordExtension(ApexPages.StandardSetController std) {
sObjList = std.getSelected();
recordIds = new Set<Id>();
for (sObject obj : sObjList) {
recordIds.add(obj.Id);
}
}
public PageReference deleteRecords() {
try {
if (sObjList.size() > 0) {
delete sObjList;
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Records deleted'));
isDeleted = true;
Schema.SObjectType sobjectType = sObjList[0].Id.getSObjectType();
String sobjectName = sobjectType.getDescribe().getName();
return new PageReference('/lightning/o/' + sobjectName + '/list');
}
} catch (Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
return null;
}
}
📄 Step 2: Create Visualforce Page
This Visualforce page will be invoked by the button and will trigger the deletion process on page load using the action attribute.
<apex:page
standardController="Stocks__c"
action="{!deleteRecords}"
extensions="DeleteRecordExtension"
recordSetVar="errorLogs"
lightningStylesheets="true">
</apex:page>
✏️ Replace
Stocks__cwith your object name if you’re working with another object.
🧭 Step 3: Add a Custom List View Button
- Go to Setup → Object Manager →
Stocks__c - Navigate to Buttons, Links, and Actions → New Button or Link
- Set these values:
- Label: Delete Selected
- Display Type: List Button
- Behavior: Display in existing window with sidebar
- Content Source: Visualforce Page
- Select Visualforce Page: the one you created above
- Click Save
🧩 Step 4: Add Button to Search Layout
- In the same object settings, go to Search Layouts for Salesforce Classic (or Search Layouts for Lightning Experience)
- Edit the List View layout
- Add your Delete Selected button to the List View Actions
- Save
✅ Final Result
Now, when you go to a list view for your object, you can:
- ✅ Select multiple records
- ✅ Click the Delete Selected button
- ✅ Delete all selected records in one go
All this happens smoothly using a Visualforce page and a touch of Apex logic behind the scenes.
Demo
📝 Wrap-Up
With just a Visualforce page, an Apex class, and a custom list view button, you’ve built your own mass delete functionality in Salesforce!
💬 Got questions? Drop a comment below. Happy building!