Document Libraries: SPList and SPFolder.Delete Differences
Removing items from a document library can be done in two different ways. Firstly, using SPList.Delete:
int iCount = spList.Items.Count;
for (int i = 0; i < iCount; i++)
{
spList.Items[0].Delete();
}
This code will delete all the items/documents in all folders, but will leave the folders undeleted. Alternatively, you may write:
int iCount = spFolder.Files.Count;
for (int i = 0; i < iCount; i++)
{
spFolder.Files[0].Delete();
}
This will delete the items/documents only in the folder represented by ‘spFolder’, and items/documents in other sub-folders will remain. The references to spList and spFolder can be obtained like this:
spList = currentWeb.Lists["MyDocumentLibrary"];
spFolder = currentWeb.Folders["MyDocumentLibrary"];
In this case, spFolder refers to the root folder in the document library.