PowerShell script to restrict deletion of SharePoint online library.
- Oct 4, 2020
- 2 min read
Updated: Oct 7, 2020

This script will come handy wherein you want to restrict users or even site collection owners to not be able to delete the SharePoint library and avoid the chance of important data loss.
Status before running script:
You can delete the library in below two ways and through PNP PowerShell:
- Navigate to site contents -> Click on ellipses of concerned library -> Choose Delete option as shown below.

- Navigate to library settings and select option "Delete this Document library" as shown below.

Script to disable the option to Delete the SharePoint library is as shown below:
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://Domain.sharepoint.com/sites/Sitename"
$DocLibName="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
$DocLibrary.AllowDeletion = $False
$DocLibrary.Update()
}
Catch {
write-host -f Red "Document library is restricted now from deletion!" $_.Exception.Message
}
Note: Please run the script below for changes to take effect. If you run only above script it will not make required changes.
In below script, we are running the same script by removing the below commands as variables had already stored the credentials and context required to perform the task:
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://domain.sharepoint.com/sites/Sitename"
$DocLibName="Documents"
Try {
#Get the Document Library
$DocLibrary = $Ctx.web.Lists.GetByTitle($DocLibName)
$Ctx.Load($DocLibrary)
$Ctx.ExecuteQuery()
#Set Allow Deletion Property to True
$DocLibrary.AllowDeletion = $False
$DocLibrary.Update()
}
Catch {
write-host -f Red "Document library is restricted now from deletion!" $_.Exception.Message
}
Checking results after running script:
Below are the changes that will take effect on SharePoint library after running above script:
- Delete option will not appear on library when accessed from Site contents as shown below:

- No option can be found with name "Delete this Document library" in library settings.

Error you will receive while deleting the list/library via PNP PowerShell will be like as shown below:

So, now we had successfully restricted a SharePoint library from being deleted by end users and Site collection administrator.
To be able to remove the library after above configuration done on certain lists or library at SharePoint online, just rerun the above mentioned script by changing below mentioned library property to "True" to allow deletion of library via GUI or programmatically.
$DocLibrary.AllowDeletion = $True
Thank you for visiting the blog, Please do share your feedback if you liked it so that I can come up with more such blogs for community.
Because, "Sharing is Caring...!!"



Comments