In this post we will discuss about how we can delete an attachment from a SharePoint list using SharePoint object model.
You can also check my previous posts on: Programmatically get SharePoint Themes [Check here
], Delete all items from SharePoint list programmatically [Check here
] and SPMonitoredScope
in SharePoint 2010.
Below is the code to delete an attachment from list item.
string listItemID = "ID of the list item";
using (SPSite site = new SPSite("http://SiteURL"))
using (SPWeb web = site.OpenWeb())
SPList list = web.Lists["MyCustomList"];
SPListItem delItem = list.GetItemById(listItemID);
SPAttachmentCollection col = delItem.Attachments;
if (delItem["AttachmentName"] != null)
string strFileName = delItem["AttachmentName"].ToString();
delItem["AttachmentName"] = string.Empty;