GitHub Icon Image
GitHub

Remove delete option on a document library

Summary

This sample script will show you how to remove the delete option on a document library to prevent users from accidentally deleting libraries if the have the "edit" permission. The script will not prevent deletions rather, just disable the UI option.

Example Screenshot

  • PnP PowerShell
  • CLI for Microsoft 365

Connect-PnPOnline -Url "https://<tenant>.sharepoint.com" -Interactive

$list = Get-PnPList -Identity "<list or library>"
$list.AllowDeletion = $false
$list.Update()

Invoke-PnPQuery
Write-Host "Done! :-)" -ForegroundColor Green

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell


$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login
}

$site = "<site>"
$list = "<list or library>"

$json = m365 spo list get --title $list --webUrl $site
$json = $json | ConvertFrom-Json
m365 spo list set --webUrl $site --id $json.Id --allowDeletion false
Write-Host "Done! :-)" -ForegroundColor Green

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Source Credit

Sample first appeared on Prevent document library deletion | CaPa Creative Ltd

Contributors

Author(s)
Paul Bullock
Adam Wójcik

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI