How can I delete a elements in secound list bounded with secand list if I delete elements in first list.

I write a code, but i don't know how to delete a bounded item, please help:

 public override void ItemAttachmentDeleting(SPItemEventProperties properties)
       if (properties.List.Title == "Wpisy")
           SPSite oSiteCollection = new SPSite("http://wasp03/");
           SPWeb witryna = oSiteCollection.RootWeb;
           SPListItemCollection firstList = witryna.Lists["Wpisy"].Items;
           SPListItemCollection secoundList = witryna.Lists["Szczegoly"].Items;
           foreach (SPListItem ofirstList in firstList)
               foreach (SPListItem osecoundList in secoundList)
                   if (ofirstList.ID.ToString() == osecoundList["ID_z_Wpisy"].ToString())



1 Answer 1


Fist of all, you should dispose oSiteCollection object. Second, if you will have a lot of items the foreach operation will be very slow, you should use CAML query to retrive items. Third, I think that you can use SPLookupField to set references between lists and use RelationShipsDeleteBehavior (also you can use this approach without any line of code, you can add this field to the list through list settings option).

  • And like whot should look CAML Query to delete ?
    – Grzegorz Z
    Commented Nov 28, 2012 at 19:52
  • 2
    you cannot delete items with CAML query, but you can retrieve items for deletion. If you would like to delete items in one time you should use batch deletion. You can see this question for more details.
    – Alexander
    Commented Nov 28, 2012 at 20:15
  • 1
    You should use batch deletion if you have really a lot of items )
    – Alexander
    Commented Nov 28, 2012 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.