11

I have an existing system that we are cleaning up with thousands of document libraries and moving the contents into Azure instead. I'm looking for a programmatic way to iterate over the lists to find ones that are empty and delete them. Does anyone have any samples (preferably using CSOM or powershell) to accomplish this that they would be willing to share?

At this point, I've come up with the following code to accomplish the task, however I'm getting the error "the underlying connection was closed: an unexpected error occurred on a receive" trying to load the lists due to a timeout because I have so many lists. Here's my solution so far hiding the user secrets of uid, pws, site.

var cred = new SharePointOnlineCredentials(uid, pws);
var context = new ClientContext(site);
context.Credentials = cred;
context.RequestTimeout = Timeout.Infinite;

var lists = context.Web.Lists;
context.Load(lists);
context.ExecuteQuery();
foreach(var list in lists)
{
    list.DeleteObject();
    list.Update();
}
Innat
  • 16,113
  • 6
  • 53
  • 101
Jim Wooley
  • 10,169
  • 1
  • 25
  • 43

2 Answers2

0

Try the code below, all you have to do here is to specify the lists you want to ignore, some system lists are listed already, make sure you are not deleting important stuff from SharePoint.

The script below uses SharePoint Online Management Shell and PnP PowerShell, download and install both before running the script:

https://www.microsoft.com/en-us/download/details.aspx?id=35588 https://github.com/SharePoint/PnP-PowerShell

cls

$url = "https://YOUR-URL-GOES-HERE.sharepoint.com"

if ($cred -eq $null)
{
    $cred = Get-Credential
    Connect-PnPOnline $url -Credentials $cred
}

$CSOM_context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$CSOM_credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password)
$CSOM_context.Credentials = $CSOM_credentials

$lists = $CSOM_context.Web.Lists
$CSOM_context.Load($lists)
$CSOM_context.ExecuteQuery()

$ignoreList = "Form Templates", "Site Assets", "Style Library", "_catalogs/hubsite", "Translation Packages"    

$lists  | ? { $_.ItemCount -eq 0 -and $_.BaseTemplate -eq 101 -and $_.Title -inotin $ignoreList}  | % {

    Write-Host "- " $_.Title -NoNewline

    try {
        Remove-PnPList -Identity $_.Title -Force 

        Write-Host -ForegroundColor Green "   [deleted] "  `n  
    }
    catch [Execption] {
        Write-Host -ForegroundColor Red   "   [FAILURE] - " $_.Exception.Message `n  
    }
}
Mr. Dr. Sushi
  • 469
  • 8
  • 22
0

I don't think so you need any timeout over here you can use below code to delete an empty document library

using Microsoft.SharePoint.Client;
using System;
using System.Linq;
using System.Security;

namespace DeleteAllEmptyDocumentLibrary
{
    class Program
    {
        static void Main(string[] args)
        {
            ListItemCollection itemCollection = null;
            SecureString pswd = new SecureString();
            try
            {
                // Site Url to scan  
                string webUrl = "https://SiteUrl";
                string userName = "UserName";
                string password = "Password";
                using (ClientContext context = new ClientContext(webUrl))
                {

                    foreach (char c in password.ToCharArray())
                        pswd.AppendChar(c);
                    // Setting credential for the above site  
                    context.Credentials = new SharePointOnlineCredentials(userName, pswd);
                    context.ExecuteQuery();
                    var lists = context.LoadQuery(context.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary));
                    context.ExecuteQuery();
                    foreach (List list in lists)
                    {
                        try
                        {
                            // Getting all items from selected list using caml query  
                            itemCollection = list.GetItems(CamlQuery.CreateAllItemsQuery());
                            //Loading selected list items  
                            context.Load(itemCollection);
                            context.ExecuteQuery();
                            // Looping each item  
                            if (itemCollection != null && itemCollection.Count == 0)
                            {
                                list.DeleteObject();
                                context.ExecuteQuery();
                            }
                        }
                        catch (Exception ex)
                        { }

                    }
                }
            }
            catch (Exception ex) { }
        }
    }
}
Negi Rox
  • 3,828
  • 1
  • 11
  • 18