Delete particular record in Visualforce table

Delete particular record in Visualforce table

There are scenarios in business environments where there is need to delete a particular record from the list of records displayed on a given table in Visualforce. For example, if there are duplicates records in your Salesforce org, one of the ways to clean the records is to delete the extra.

The table below shows the desired user interface to be achieved

The UI mimics the look and feel of Salesforce list of records in that most records owned by you have the Edit and Del links displayed.

The Visualforce page code is pasted below –

<apex:page controller="deleteController">
 <style type="text/css">
  #title {
  font-size: 150%;
  margin-left: 30%;
  <h2 id="title">Delete Banks Records</h2><br/><br/>
    <apex:form >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!bankList}" var="b" id="theTable">
            <apex:column headervalue="Bank Name">
                <apex:OutputText value="{!b.Name}" /> 
            <apex:column headervalue="Source">
                <apex:OutputText value="{!b.Source__c}" />
            <apex:column headervalue="Services">
                <apex:inputText value="{!b.Services__c}" />
            <apex:column headervalue="Region">
                <apex:inputText value="{!b.Region__c}" />
           <apex:column >                      
              <apex:facet name="header">Action</apex:facet>
                <apex:commandLink action="{!deleteRecord}" value="Del" onclick="if(!confirm('Are you sure?')) return false;">
                 <apex:param name="rowId" assignTo="{!bkId}" value="{!b.Id}"/>                 


Depending on where you want your Action column to appear,  it can come first or last as shown above.

The custom controller used along with the Visualforce code is attached below

public class deleteController {

   public String bkId {get; set;}  
   public List<Bank__c> bankList {get{
   return [ select Id, Name, Source__c, Services__c, Region__c from Bank__c Limit 5];
   } set;} 

 public deleteController() {     
      delbank = new Bank__c();
     //bankList = [ Select Id, Name, Source__c, Services__c, Region__c from Bank__c Limit 10];
   public PageReference save() {
     update bankList;
     return null;
   public PageReference deleteRecord(){
        Bank__c remBank = [select id from Bank__c where id =: bkId limit 1];        
        if(remBank != null)
                delete remBank;
            catch(DmlException ex){
       return null;


Notice that  <apex:commandlink>  rerender attribute is not used in the Visualforce code. This is because of the way the  List<Bank__c>  property is defined in the Apex code.  Instead of using automatic property whereby you leave the get and set accessor code blocks empty, the get accessor returns the SOQL query needed to display the list of records for the table. If you write the SOQL query in the constructor (it is commented out in the code), the particular record you want to delete will get deleted but the table will not re-render. That means you will have to refresh the table/page manually or use the rerender attribute


Apex Developer Guide

Visualforce Developer Guide