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 they have the "edit" permission. The script will not prevent deletions rather, just disable the UI option.

Example Screenshot

  • PnP PowerShell
  • CLI for Microsoft 365

# Display name of SharePoint online list or document library
$libraryName = "My Document Library"

# SharePoint online site URL
$siteUrl = Read-Host -Prompt "Enter your SharePoint site URL (e.g https://contoso.sharepoint.com/sites/work)"

# Connect to SharePoint online site
Connect-PnPOnline -Url $siteUrl -Interactive

# Remove delete option on a document library
Set-PnPList -Identity $libraryName -AllowDeletion $false

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

# Disconnect SharePoint online connection
Disconnect-PnPOnline

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

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


# Display name of SharePoint online list or document library
$libraryName = "My Document Library"

# SharePoint online site URL
$siteUrl = Read-Host -Prompt "Enter your SharePoint site URL (e.g https://contoso.sharepoint.com/sites/work)"

# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login
}

# Remove delete option on a document library
m365 spo list set --webUrl $siteUrl --title $libraryName --allowDeletion false

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

# Disconnect SharePoint online connection
m365 logout

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

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

Source Credit

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

Contributors

Author(s)
Paul Bullock
Adam Wójcik
Ganesh Sanap

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