1

I want to delete folder and their content in document library

Structure:

Document Library
-- Folder(delete this)
--- image.jpg(delete this)

This is my Code:

SPFolder myFolder = oWebBG.GetFolder(urlFolderSmallEN);
myFolder.Recycle();

When I delete the folder, it will delete its contents as well. I have no subfolders.

1

You can refer below code to delete folder or sub-folder form document library

String siteURL = "http://<<SharePoint URL>>";
String libraryName = "<<LibraryName>>";
String folderToDelete = "<<FolderName>>";

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

        foreach (SPFolder folder in folders)
        {
            if (folder.Name == folderToDelete)
            {
               web.Folders[libraryName ].SubFolders.Delete(folder.Url);
            }
        }                   
    }
}
0

This code works good for my case:

 if (FolderExists(urlFolder, oWeb))
 {
     SPFolder myFolder = oWeb.GetFolder(urlFolder);
     myFolder.Recycle(); //delete folder and content
 }

Check Folder exist:

        static bool FolderExists(string url, SPWeb web)
        {
           try
           {
              if (web.GetFolder(url).Exists)
              {
                   return true;
              }
              return false;
           }
           catch (ArgumentException)
           {
                return false;
           }
       }

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.