SharePoint Online: PowerShell to Delete a Document Library
Requirement: Delete SharePoint Online Document Library using PowerShell.
How to delete a document library in SharePoint Online?
Document libraries are a vital part of SharePoint Online and are ideal for storing files you need to access frequently. However, there may be times you want to delete a document library that you no longer need. How to delete a document library in SharePoint Online? Well, This can be done relatively quickly, but it’s important to remember that all the files and folders stored in the library will also be deleted. So, be sure to back up any important documents first!
To delete a document library in SharePoint Online, follow these steps:
- Assuming you have Edit permissions: Login to SharePoint Online and navigate to the document library.
- Click on Settings Gear >> Choose “Library settings”.
- Under Permissions and Management, click on the “Delete this document library” link.
- Confirm the prompt, “This document library will be removed, and all its files will be deleted. Are you sure you want to send this document library to the site Recycle Bin?” with “Yes” once.
This sends the document library and all its contents to the recycle bin. This may take a few moments, depending on the size of the library and the number of items it contains. You can also remove a document library in SharePoint Online from the “Site Contents” page. Hover over the document library, and click on “Remove” from the context menu to delete the document library.
Deleting a document library is a straightforward process and can be accomplished in just a few clicks. Alright, let’s delete a SharePoint Online document library using PowerShell!
SharePoint Online: PowerShell to Delete a Document Library
You can use PowerShell to delete a document library in SharePoint Online. First, connect to your SharePoint Online site using PowerShell. Then, retrieve the document library from the site. Finally, use delete or recycle methods to delete the document library you want to remove.
Here is the PowerShell to delete a document library in SharePoint Online:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com"
$DocLibName="Docs"
Try {
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials
#Get the Document Library
$DocLibrary = $Ctx.web.Lists.GetByTitle($DocLibName)
$Ctx.Load($DocLibrary)
$Ctx.ExecuteQuery()
If($DocLibrary -ne $Null)
{
#Delete the Document Library - Send to Recycle bin
$DocLibrary.Recycle()
#To Delete the Document Library - Permanently
#$DocLibrary.DeleteObject()
$Ctx.ExecuteQuery()
Write-Host "Document Library Deleted Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Deleting Document Library!" $_.Exception.Message
}
This will delete the library from your SharePoint Online account. Please note, the “Recycle()” method deletes and sends the document library to the recycle bin, and the “DeleteObject()” method permanently deletes the document library from SharePoint Online.
Unable to delete a document library in SharePoint Online?
Cannot delete the document library? Are you getting the “This list cannot be deleted.” error message when trying to delete a document library, or not getting the “Delete this document library” link in document library settings? Well, to delete this sort of document library, you should set: the “AllowDeletion” property to “$True” first and then delete the document library.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Config Parameters
$SiteUrl = "https://crescent.sharepoint.com/"
$DocLibName="Project Documents"
Try {
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $credentials
#Get the Document Library
$DocLibrary = $Ctx.web.Lists.GetByTitle($DocLibName)
$Ctx.Load($DocLibrary)
$Ctx.ExecuteQuery()
#Set Allow Deletion Property to True to enable delete option
$DocLibrary.AllowDeletion = $True
$DocLibrary.Update()
If($DocLibrary -ne $Null)
{
#Delete the Document Library - Send to Recycle bin
$DocLibrary.Recycle()
#To Delete the Document Library - Permanently
#$DocLibrary.DeleteObject()
$Ctx.ExecuteQuery()
Write-Host -f Green "Document Library Deleted Successfully!"
}
}
Catch {
write-host -f Red "Error Deleting Document Library!" $_.Exception.Message
}
PnP PowerShell to Delete SharePoint Online Document Library
Use the Remove-PnPList cmdlet to remove a document library in SharePoint Online. Here is the PnP PowerShell script to delete a document library in SharePoint Online. Be sure to replace the Site URL and Library Name variable according to your tenant.
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$LibraryName ="Work"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Check if document Library exist
If(Get-PnPList -Identity $LibraryName)
{
#powershell to remove document library
Remove-PnPList -Identity $LibraryName -Force -recycle
Write-host -f Green "Document Library '$LibraryName' Deleted Successfully!"
}
Else
{
Write-host -f Yellow "Could not find Library '$LibraryName'"
}
This script deletes the document library and sends it to the recycle bin! These scripts can be used for SharePoint lists as well. Just supply the List name to the variable. These PowerShell scripts can be run using Visual Studio Code or Windows PowerShell ISE.
Delete All Empty Document Libraries in SharePoint Online using PowerShell
This time, we’ve got a requirement to remove all empty document libraries from a site! If you’re like me, you’ve probably created too many document libraries in SharePoint Online. And if you’re like me, you also probably want to clean them up occasionally. PowerShell cmdlets to the rescue!
#Parameter
$SiteURL = "https://Crescent.sharepoint.com/"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
$ExcludedLibraries = @("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates", "Site Collection Documents","Site Collection Images",
"Reporting Templates","Solution Gallery", "Style Library", "Web Part Gallery","Site Assets", "wfpub", "Site Pages", "Images")
#Get All Empty Document Libraries to Delete
$EmptyLibraries = Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $ExcludedLibraries -notcontains $_.Title -and $_.ItemCount -eq 0}
#Delete each empty document library
ForEach($Library in $EmptyLibraries)
{
#Delete document library in sharepoint online using powershell
#Remove-PnPList -Identity $Library -Recycle -Force
Write-host -f Green "Library '$($Library.Title)' Deleted Successfully!"
}
I’ve commented on Line#17 intentionally. Just run the script once and make sure no other important document libraries are about to be deleted, and uncomment the line (by removing # from it!) to delete the empty document library. If you want to exclude any empty library from deletion, just add its name to the $ExcludedLibraries array.
Getting the “The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator” error on deleting a document library with more than 5000 files? Well, You have to delete all files from the SharePoint Online document library first!
To delete a SharePoint Online list, navigate to the list >> Click on “Settings” gear >> List settings >> Select “Delete this list” link. Confirm the deletion by clicking “OK”. If you do not see the “Delete this list” option, you may not have permission to delete the list. You can also delete a list from its context menu on the “Site contents” page.
More Info: How to delete a list in SharePoint Online?
Navigate to your SharePoint Online document library in the browser >> Click the check mark to select all files >> Click on the “Delete” button on the top link bar to remove all the files. Click the “Delete” button on the Delete dialog box to confirm the deletion.
More info: PowerShell to delete all files in SharePoint Online Document Library
To get to the Recycle bin in SharePoint Online, Login to SharePoint Online Site >> Click on the settings gear >> Site Contents. On the site contents page, click on “Recycle bin” to get into the recycle bin. For the second-stage recycle bin, scroll to the bottom and click on the “Second-stage recycle bin” link.
To delete all list items in a SharePoint Online list, use the PowerShell script: Bulk delete SharePoint Online list items using PowerShell
I have a document library of 600 000 items. It cant be deleted this way there are some limitations. (it cannot be deleted via GUI either) I am also trying this: Get-PnPListItem -List $LibraryName -Fields “ID” -PageSize 100 -ScriptBlock { Param($items) $items | Sort-Object -Descending | ForEach-Object{ $_.DeleteObject() } }
It worked for a some thousands, but now I am hitting some sort of treshold again.
Any ideas?
Yes. Either deleting or moving items to a different library to reduce its count is the only way to delete larger lists and document libraries. Try the PnP-Batch method: How to Delete All Items in a SharePoint Online List using PowerShell?
Hi, is there a way instead of adding document libraries to exclude that we can exclude any libraries with files in it? 4000 libraries with around 50% having documentation in them make this a rather laborious task. Unless there was a way to run a script for a full library breakdown including child count?
The last script checks for Item count as well and proceeds only when the document library is empty!
Salaudeen, thats brilliant. I am using SP2019 OnPrem. What changes would I need to make to get this to run. As you can probably establish, I am not the most technical of people.
I really appreciate your help with this.
Hi Dom,
This post is written for SharePoint Online! For On-premises, You can refer How to Delete a Document Library in SharePoint using PowerShell?
Great script, it works!