Managing document versions in SharePoint is crucial for maintaining an organized and efficient workspace. Over time, document libraries can accumulate numerous versions, consuming storage space and complicating file management. This article provides a step-by-step guide on how to delete previous versions in a SharePoint document library using PowerShell.
Prerequisites
Before you begin, ensure you have the following:
- Access to the SharePoint site and document library.
- PowerShell installed on your machine.
- Necessary permissions to execute scripts and manage SharePoint libraries.
Step 1: Install the PnP PowerShell Module
The PnP PowerShell module is essential for interacting with SharePoint Online. Install the module using the following command:
Install-Module -Name PnP.PowerShell
If you are using PowerShell 7 or later, you might need to install it specifically for that version:
Step 2: Import the PnP PowerShell Module
After installing the module, import it into your session:
Step 3: Connect to SharePoint Online
Connect-PnPOnline
cmdlet to connect to your SharePoint site. Replace the placeholders with your actual SharePoint site URL and credentials:You will be prompted to enter your credentials.
Step 4: Delete Previous Versions in a Document Library
Once connected, you can use the following script to delete previous versions in a specified document library. Replace "YourLibraryName"
with the actual name of your document library:
This script retrieves all items in the specified document library and deletes all previous versions of each file.
Troubleshooting Common Issues
If you encounter errors such as CommandNotFoundException
, ensure the PnP PowerShell module is installed correctly and imported into your session. Verify the installation with:
Get-Module -ListAvailable -Name PnP.PowerShell
Ensure you are using the latest version of PowerShell by checking your version:
Alternative Solutions
If PowerShell isn't your preferred method, consider these alternatives:
- Power Automate: Create a custom flow to automatically delete older versions based on specific criteria.
- Manual Deletion: Use the SharePoint interface to manually delete versions via the Version History option.
- SharePoint Trim Versions Feature: Utilize SharePoint's built-in feature to trim versions based on age or count limits.
By following these steps, you can efficiently manage document versions in your SharePoint libraries, ensuring a streamlined and organized workspace.
USING POWER AUTOMATE
Implementing the deletion of previous versions in a SharePoint document library using Power Automate can help automate and streamline the process. Here’s a step-by-step guide to create a Power Automate flow for this task:
Step-by-Step Guide to Create a Power Automate Flow
1. Create a New Flow
- Go to Power Automate.
- Click on Create and select Instant cloud flow.
- Name your flow and choose the trigger Manually trigger a flow. Click Create.
2. Get Files from the Document Library
- Add a new action Get files (properties only).
- Configure the action:
- Site Address: Select your SharePoint site.
- Library Name: Select your document library.
3. Loop Through Each File
- Add an Apply to each action.
- Set the value from the Get files (properties only) action as the output to loop through each file.
4. Get File Versions
- Inside the Apply to each action, add a Send an HTTP request to SharePoint action.
- Configure the action:
- Site Address: Select your SharePoint site.
- Method: GET
- Uri:
_api/web/lists/getbytitle('YourLibraryName')/items(@{items('Apply_to_each')?['ID']})/versions
- This action retrieves all versions of each file.
5. Delete Previous Versions
- Add another Apply to each action inside the first one to loop through each version.
- Set the value from the Send an HTTP request to SharePoint action as the output.
- Add a Condition to check if the version is not the current version.
- Expression:
@not(equals(items('Apply_to_each_2')?['IsCurrentVersion'], true))
- Expression:
- In the If yes branch, add a Send an HTTP request to SharePoint action to delete the version.
- Site Address: Select your SharePoint site.
- Method: DELETE
- Uri:
_api/web/lists/getbytitle('YourLibraryName')/items(@{items('Apply_to_each')?['ID']})/versions(@{items('Apply_to_each_2')?['ID']})
Summary
This flow will loop through each file in the specified document library, retrieve all versions, and delete all previous versions while keeping the current version intact. This automation helps maintain a clean and efficient document library without manual intervention.
If you have any questions or need further assistance, feel free to reach out! 😊