Delete all attachments from SharePoint 2016/2013 list [PowerShell and Browser]

This SharePoint tutorial explains various ways to delete all attachments from SharePoint 2013/2016 list. We will see how we can delete all attachments from SharePoint list using PowerShell, using a browser.

We will also discuss, how to delete all attachments from the SharePoint list by ID using the SharePoint server object model.

The attachment will not be item specific, it will remove all attachments from the SharePoint list.

Here I have a SharePoint list name as AttachmentTest which contains title columns and the attachment column. And each item has attachments like below:

delete all attachments from sharepoint list

Now here my requirement is to delete all attachments from the whole list. I should not go item by item and delete those attachments because the list is a very huge list. Another requirement is that once all the attachments got deleted they should be able to see add attachments to items. Means user can go to any item and can attach documents to it.

Delete all attachments from SharePoint list from the browser itself

We can delete all attachments from the SharePoint list by using one out of box approach. Open the SharePoint list, then go to the list settings page. Then from the General Settings click on Advanced settings. Here go to the Attachments section, click on the Disabled radio button as shown in the fig below:

delete all attachments from sharepoint list programmatically

This will as ask a confirmation message like the below. simply click on OK as shown in the fig below:

delete all attachments from sharepoint list using powershell

Now if you will open your list you will not see any attachment associated with any item.

delete all attachments from sharepoint 2016 list

Now again if you want to enable the attachment, so that the user can add an attachment, then Open your list settings again.

Then from the General Settings click on Advanced settings. Here go to the Attachments section, click on Enable as shown in the fig below:

delete all attachments from sharepoint 2013 list

Delete all attachments from SharePoint list using PowerShell

Below is the PowerShell command which will delete all attachments from SharePoint 2013/2016 list. If you are new to PowerShell, read an article on Working with PowerShell in SharePoint Online/2016/2013.

$web=Get-SPWeb -identity “http://SiteURL”
$list=$web.lists[“ListName”];
foreach($item in $List.Items)
{
if( $Item[“Attachments”] -eq “True” )
{
for($i=$item.Attachments.count-1; $i -ge 0; $i–)
{
$Item.Attachments.Delete($item.Attachments[$i])
}
}
$item.update()
}

The above PowerShell script you can write, test and debug using Visual Studio Code or Windows PowerShell ISE.

Delete All Attachments from SharePoint 2013 list item by ID using Server object model code

This SharePoint tutorial, we will discuss, how we can delete attachments from a SharePoint list using the SharePoint server object model. In this particular example, we will delete an attachment from a list item using the SharePoint server object model code.

Below is the full code which will delete all attachments from the list whose item id is 5. You can develop a visual web part in SharePoint and can call the below code from a button click.

public static void DeleteAllAttachementsFromList()
{
using (SPSite site = new SPSite(“http://SiteURL”))
{
using (SPWeb web = site.OpenWeb())
{
SPListItem listItem = web.Lists[“ListName”].GetItemById(5);
List<string> fileNames = new List<string>();
foreach (string fileName in listItem.Attachments)
{
fileNames.Add(fileName);
}
foreach(string fileName in fileNames)
{
listItem.Attachments.Delete(fileName);
}
listItem.update();
}
}
}

You can also pass the list item id as a method parameter like below:

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;
Col.Delete(strFileName);
delItem.Update();
}
}
}

We saw how to delete all attachments from the SharePoint list by ID using the SharePoint server object model.

You may like following SharePoint list and document library tutorials:

This SharePoint tutorial, we learned how to delete all attachments from a SharePoint list using PowerShell, using a browser and using the SharePoint server object model code.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>