0

I want to delete(clear) content of document libraries and lists from my site.

  • my document libraries have folders and items inside them.
  • my list have items

How to do this:

This is my code:

                 foreach (SPList list in fundListAndLibs)
                 {
                     if (list.BaseType == SPBaseType.DocumentLibrary)
                     {
                         foreach (SPListItem item in list.Items)                           
                         {
                            ///I cannot delete folder with this             
                           //item.Folder.Delete();

                             //This delete only items in folders
                             item.File.Delete();    
                         }
                     }
                     if (list.BaseType == SPBaseType.GenericList)
                     {
                         foreach (SPListItem item in list.Items)
                         {
                             list.GetItemById(item.ID).Delete();
                         }
                     }
                 }
  • are you tried item.File.Delete(); within foreach of doc library – Mohamed El-Qassas MVP Jan 4 '17 at 18:45
  • See my code ... Will it really work? – Goshky Jan 4 '17 at 19:11
  • Please write this in response to accept it. – Goshky Jan 4 '17 at 19:19
  • glad to hear it's worked :) – Mohamed El-Qassas MVP Jan 4 '17 at 19:21
2

To delete items within document library , try the following

foreach (SPListItem item in list.Items)
         {
            item.File.Delete();    
         }
       }

To delete folders with its subfolders and items, try the following

String siteURL = "http://Qassas";
                String docName = "Qassas";


                using (SPSite site = new SPSite(siteURL))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPFolderCollection folders = web.Folders[docName].SubFolders;

                        foreach (SPFolder folder in folders)
                        {
                            if (folder.Url != docName + "/Forms")
                            {
                                web.Folders[docName].SubFolders.Delete(folder.Url);
                            }
                        }

                    }
                }
  • This only delete items in folders, i want delete AND folders... – Goshky Jan 4 '17 at 19:31
  • 1
    let me apply something in my side and come back to you :) – Mohamed El-Qassas MVP Jan 4 '17 at 19:43
  • 1
    I have updated my answer , and sorry for late response because I was tried it first in my side , hope to help you :) – Mohamed El-Qassas MVP Jan 4 '17 at 20:20
  • 1
    With this work fo me --> SPFolderCollection folders = web.Folders[web.Url + "/" + list.RootFolder.Url].SubFolders; – Goshky Jan 4 '17 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.