2013-06-13

Suppose one SPList  has SPLookUp field which has Relationship with other list Field, when deleting the list it will give error, for this first need get the child list and related Field from the parent list

Then delete the related fields from the child list after this delete the child list , now you can delete the parent list, below is the code:

private static void DeleteLikeTracking(SPList parentList)

        {

            SPList childList = parentList.ParentWeb.Lists[“ChildList”];

            SPRelatedFieldCollection relatedFields = parentList.GetRelatedFields();

            foreach (SPRelatedField field in relatedFields)

            {

                SPField relatedField = childList.Fields[field.FieldId];

                if(relatedField !=null)

                    childList.Fields[field.FieldId].Delete();

                childList.Update();

            }

            childList.Delete();

parentList.Delete();

        }

 

About the author 

Anil Lakhagoudar