How To Remove All Document Libraries From SharePoint Site Using C#

Deleting the entire document library in the site manually is difficult and takes more time to process. This blog will help you learn how to automate the deletion process for the entire site. 

Step 1 - Create a project for implementation.
  1. Create a new project in “Microsoft Visual Studio”.

    How Can Remove All Document Libraries From SharePoint Site Using C#

  2. Choose Console App (.NET Framework) in the pop-up and then click OK to create the project.

    How Can Remove All Document Libraries From SharePoint Site Using C#

Step 2 - Install packages for the project

  1. Go to the “Tools”, click “NuGet Package Manager” from Tools, and choose “Manage NuGet Packages for solution ” in the NuGet Package Manager.

    How Can Remove All Document Libraries From SharePoint Site Using C#

  2. If you want to delete the libraries from SharePoint 2013, then install “SharePointPnPCore2013”; else, you want to delete from SharePoint Online. Install “SharePointPnPCoreOnline”.

    How Can Remove All Document Libraries From SharePoint Site Using C# 

Step 3 - Code Implementation

If you want to delete the libraries from SharePoint Online, follow the below sample code.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. namespace ConsoleApp {  
  8.     classProgram {  
  9.         staticvoid Main(string[] args) {  
  10.             DeleteLibraries_SharePointOnline("enter your username"" enter your password");  
  11.         }  
  12.         public static void DeleteLibraries_SharePointOnline(string username, string password) {  
  13.             using(ClientContext clientcontext = new ClientContext("https://SiteUrl")) {  
  14.                 clientContext.Credentials = newSharePointOnlineCredentials(username, password);  
  15.                 clientcontext.Load(clientcontext.Web.Lists);  
  16.                 clientcontext.ExecuteQuery();  
  17.                 var lists = clientcontext.Web.Lists;  
  18.                 foreach(List list in lists.ToList()) {  
  19.                     if (list.BaseType.ToString() == "DocumentLibrary") {  
  20.                         Console.WriteLine(list.Title);  
  21.                         list.DeleteObject();  
  22.                         clientcontext.ExecuteQuery();  
  23.                     }  
  24.                 }  
  25.             }  
  26.         }  
  27.     }  
  28. }  

If you want to block a library from the deletion process, make changes like in the following code to skip that particular library.

  1. if (list.BaseType.ToString() == "DocumentLibrary" && list.Title != "Important Library Name")  
  2. {   
  3.    Console.WriteLine(list.Title);  
  4.    list.DeleteObject();  
  5.    clientcontext.ExecuteQuery();   
  6. }  

Step 4 - Run the Project

Click “Start” to run the project. The name of the libraries to be deleted get displayed in the console.

I hope you learned how to remove all document libraries in SharePoint using C#.