Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently delete all files in a document library?

I am looking for a clear, complete example of programmatically deleting all documents from a specific document library, via the Sharepoint object model. The doclib does not contain folders. I am looking to delete the documents completely (ie I don't want them in the Recycle Bin).

I know of SPWeb.ProcessBatchData, but somehow it never seems to work for me.

Thanks!

like image 307
Paul Lalonde Avatar asked Oct 31 '08 20:10

Paul Lalonde


People also ask

How do I delete all files from a SharePoint library?

Navigate to your SharePoint Online document library in the browser, hover over the header, and click the check mark to select all files. Click on the “Delete” button on the top link bar. On the Delete dialog box, confirm the delete operation by clicking the “Delete” button.

How do I delete all files in My Documents?

Locate the file that you want to delete. Right-click the file, then click Delete on the shortcut menu. Tip: You can also select more than one file to be deleted at the same time. Press and hold the CTRL key as you select multiple files to delete.

How do I delete multiple files in SharePoint?

Open the SharePoint document library where you want to delete items. To select one or more items that you want to delete, hover over the folder, and then select the checkbox. Right-click a file, folder, or link icon, and then select Delete. In the Delete confirmation dialog, click OK.


1 Answers

I would persevere with the ProcessBatchData approach, maybe this will help:

Vincent Rothwell has covered this best: http://blog.thekid.me.uk/archive/2007/02/24/deleting-a-considerable-number-of-items-from-a-list-in-sharepoint.aspx

Otherwise I'm not sure the other recommendation will work, as a Foreach loop will not like that the number of items in the collection changes with each delete.

You are probably best placed doing a reverse for loop (I didn't test this code, just an example):

for (int i = SPItems.Length - 1; i >= 0; i--)
{
    SPListItem item = SPItems[i];
    item.File.Delete();
}
like image 171
Daniel McPherson Avatar answered Oct 15 '22 19:10

Daniel McPherson