3

how to delete all files in subfolder in office 365 using CSOM C#. I have code like particular file only but I need delete all files at a time.

1
  • 1
    Well if you know how to delete one file, you can get all files in a folder and iterate over them and delete the files one by one.
    – yannisgu
    Commented May 1, 2014 at 8:38

3 Answers 3

2

@yannisgu is correct to state that @mvp should know how to do this for several files. But this might not be the case, as @mvp might know about the Web.GetFileByServerRelativeUrl method which is specifically designed to retrieve one single file, and does not immediately show any alternative approach to get several files.

One alternative way to get all the files of a subfolder for deletion is to go through RootFolder, like this:

var files =
clientContext.Web.Lists.GetByTitle("TestLib").
    RootFolder.Folders.GetByUrl("TestFolder").Files;
clientContext.Load(files);
clientContext.ExecuteQuery();

files.ToList().ForEach(file => file.DeleteObject());
clientContext.ExecuteQuery();

Or you could delete the whole folder in one single move:

clientContext.Web.Lists.GetByTitle("TestLib").
    RootFolder.Folders.GetByUrl("TestFolder").DeleteObject();
clientContext.ExecuteQuery();

Update: If you delete the whole folder you will loose custom permissions, deleting all files is most likely a better approach.

2

Probably the most simple way would be to remove the Folder that contains the Files. In the below example Web.GetFolderByServerRelativeUrl method is used to return the folder object located at the specified server-relative URL:

public static void DeleteFolder(Web web,string folderRelativeUrl)
{
     var folder = web.GetFolderByServerRelativeUrl(folderRelativeUrl);
     folder.DeleteObject();
     web.Context.ExecuteQuery();
}       

However if you need to preserve Folder, then you could retrieve Files from Folder using Folder.Files property and delete them:

    public static void DeleteFiles(string url, SharePointOnlineCredentials credentials,string  folderRelativeUrl)
    {  
        using (var context = new ClientContext(url))
        {
            context.Credentials = credentials;
            var folder = context.Web.GetFolderByServerRelativeUrl(folderRelativeUrl);
            context.Load(folder.Files);
            context.ExecuteQuery();
            folder.Files.ToList().ForEach(file => file.DeleteObject());
            context.ExecuteQuery();
        }
    } 
8
  • 1
    Downside is that even if you recreate the folder, you may loose broken inheritance and special permissions Commented May 1, 2014 at 16:31
  • This is a better answer than mine, but I'll leave mine to show alternatives. Also, can you really initialize ClientContext like that for 365? (I will up when you altered according to Robert :) )
    – eirikb
    Commented May 1, 2014 at 17:47
  • Thank you guys for your comments, appreciate it. @eirikb, you're right, to access O365, credentials should be provided as demonstrated in the second example. Commented May 1, 2014 at 18:36
  • Nice, but just wondering, if you were deploying an autohosted app wouldn't the initialization be different from your second example?
    – eirikb
    Commented May 1, 2014 at 18:43
  • That's right, for autohosted app it would be different Commented May 2, 2014 at 6:26
0

Use GetFolderByServerRelativeUrl to load the subfolder and then use DeleteObject().

Example:

string LibraryName = "Documents";
string siteUrl = "https://YourSite.com/sites/Demo";

SecureString Password = new SecureString();
foreach (char c in "YourPassword")
{
     Password.AppendChar(c); 
}

using (ClientContext ctx = new ClientContext(siteUrl))
{
    ctx.Credentials = new SharePointOnlineCredentials("YourUserName",Password)
}


var folder = rootWeb.GetFolderByServerRelativeUrl("Shared%20Documents/MainFolder/SubFolder1/SubFolder2");
ctx.Load(folder);
folder.DeleteObject();

ctx.ExecuteQuery();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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