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!
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.
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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With