SharePoint Online: How to Delete a Document Library using PowerShell?
Requirement: Delete a Document Library in SharePoint Online using PowerShell
How to Delete a Document Library in SharePoint Online?
To delete a document library in SharePoint Online, follow these steps:
PowerShell to Delete a Document Library in SharePoint Online
Let's delete a document library in SharePoint Online using PowerShell
Delete SharePoint Online Document Library using PnP PowerShell
Lets delete document library in SharePoint Online with PowerShell PnP
How to Delete a Document Library in SharePoint Online?
To delete a document library in SharePoint Online, follow these steps:
- Login to your SharePoint Online site >> Click on Settings gear >> Select Site contents from the menu.
- On the site contents page, Hover over the document library you want to delete and then click the context menu.
- From the menu, click on "Remove" and then confirm the prompt to send the document library to the Recycle Bin.
- Alternatively, you can remove a document library in SharePoint Online by going to library settings >> Click on "Delete this Document Library" under Permissions and Management group.
PowerShell to Delete a Document Library in SharePoint Online
Let's delete a document library in SharePoint Online using PowerShell
#Custom function to delete a SharePoint Online document library using powershell Function Delete-SPODocumentLibrary { param ( [string]$SiteURL = $(throw "Please Enter the Site URL!"), [string]$LibraryName = $(throw "Please Enter the Library Name to Delete!") ) Try { #Get Credentials to connect $Cred= Get-Credential $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = $Credentials #Get the Document Library and set allow deletion flag $Library=$Ctx.Web.Lists.GetByTitle($LibraryName) $Library.AllowDeletion = $True $library.Update() $Ctx.ExecuteQuery() #Delete the Document Library - Send to Recycle bin $Library.Recycle() | Out-Null #To permanently delete, call: $Library.DeleteObject() $Ctx.ExecuteQuery() Write-Host "Document Library: '$LibraryName' has been Deleted Successfully!" -ForegroundColor Green } Catch { write-host -f Red "Error Deleting Document Library!" $_.Exception.Message } } #Call the function to delete a document library Delete-SPODocumentLibrary -SiteURL "https://crescenttech.sharepoint.com/sites/Marketing/" -LibraryName "Team Documents"Once deleted, they are moved in to the recycle bin and if necessary, we can restore them as long as they remain in the recycle bin (Technically, 93 days!)
Delete SharePoint Online Document Library using PnP PowerShell
Lets delete document library in SharePoint Online with PowerShell PnP
#Config Variables $SiteURL = "https://crescenttech.sharepoint.com/sites/Marketing" $LibraryName="Branding" #Connect to PnP Online Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential) #Get the Library to Delete $Library = Get-PnPList -Identity $LibraryName -ErrorAction SilentlyContinue #Check if Library exists If($Library) { #sharepoint online powershell to delete library Remove-PnPList -Identity $LibraryName -Recycle -Force Write-host -f Green "Library '$LibraryName' Deleted Successfully!" } Else { Write-host -f Yellow "Could not find Library '$LibraryName'" }
Thanks!
ReplyDelete