How to delete event receiver from a SharePoint list

In  Hot wo add event receiver to a SharePoint list, we saw how we added an event receiver to a list. Now, we will see how
to delete the existing event receiver on a list.

  1. private void DeleteEventReceiverFromAList(string siteUrl)  

  2.     {  

  3.         using (SPSite site = new SPSite(siteUrl))  

  4.         {  

  5.             using(SPWeb web = site.OpenWeb())
     

  6.             {  

  7.                 try 

  8.                 {  

  9.                     SPList list = web.Lists["myList"];  

  10.                     if (list != null)  

  11.                     {  

  12.                         string className = "EventReceiverClass";  

  13.                         string asmName = "EventReceiverAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a865f0ecc234ea51";
     

  14.                         web.AllowUnsafeUpdates = true;  

  15.  

  16.                         int receivers = list.EventReceivers.Count;
     

  17.                         bool isAddedReceiverExist = false;  

  18.                         bool isUpdatedReceiverExist = false;  

  19.                         for (int i = 0; i < receivers; i++)
     

  20.                         {  

  21.                             SPEventReceiverDefinition eventReceiver = list.EventReceivers[i];
     

  22.                             if (eventReceiver.Class == className && eventReceiver.Type == SPEventReceiverType.ItemAdded)
     

  23.                             {  

  24.                                 eventReceiver.Delete();  

  25.                                 break;  

  26.                             }  

  27.                         }  

  28.                     }  

  29.                 }  

  30.                 catch { }
     

  31.                 finally 

  32.                 {  

  33.                     web.AllowUnsafeUpdates = false;  

  34.                 }  

  35.             }  

  36.         }  

  37.     } 
In this code also, there is nothing to explain very detail. Please let me
know if you have any questions.