Managing file versions in
SharePoint Online is essential to maintain storage hygiene and performance,
especially when versioning is enabled for document libraries. Over time, older
versions of files can accumulate and consume significant storage space. This
PowerShell script demonstrates how to connect to a SharePoint Online site
and delete all previous versions of files from a specified document
library.
- PnP PowerShell Module installed (Install-Module -Name
"PnP.PowerShell")
- Permissions to access the SharePoint Online site
and document library
- SharePoint versioning must be enabled for the
document library
📝 Script Overview
📝 What the Script Does
- Connects to the specified SharePoint Online site using PnP PowerShell.
- Retrieves all file items from the specified document library (excluding
folders).
- Loads each file’s version history.
- Deletes all previous versions for each file, keeping only the latest one.
⚠️ Important Notes
- This script permanently deletes all
previous versions. Make sure this is what you intend before running it.
- Test the script in a non-production environment
first.
- You may want to add logging or backups depending
on your organization’s governance policies.
🧠 Use Cases
- Reclaiming storage space in libraries with heavy
versioning.
- Maintaining SharePoint Online quota limits.
- Cleaning up outdated versions during migration or audits.
No comments:
Post a Comment