Monday, April 28, 2025

How to Delete Previous Versions in a SharePoint Document Library Using PowerShell

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:

Install-Module -Name PnP.PowerShell -Scope CurrentUser

Step 2: Import the PnP PowerShell Module

After installing the module, import it into your session:

Import-Module PnP.PowerShell

Step 3: Connect to SharePoint Online

Use the Connect-PnPOnline cmdlet to connect to your SharePoint site. Replace the placeholders with your actual SharePoint site URL and credentials:
Connect-PnPOnline -Url "https://yoursharepointsite.sharepoint.com" -Credentials (Get-Credential)

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:

$library = "YourLibraryName"
$files = Get-PnPListItem -List $library
foreach ($file in $files) {
    Remove-PnPFileVersion -List $library -Identity $file.Id -AllVersions

}

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:

$PSVersionTable.PSVersion

Restarting PowerShell after installing a module can also resolve recognition issues.

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

  1. Go to Power Automate.
  2. Click on Create and select Instant cloud flow.
  3. Name your flow and choose the trigger Manually trigger a flow. Click Create.

2. Get Files from the Document Library

  1. Add a new action Get files (properties only).
  2. Configure the action:
    • Site Address: Select your SharePoint site.
    • Library Name: Select your document library.

3. Loop Through Each File

  1. Add an Apply to each action.
  2. Set the value from the Get files (properties only) action as the output to loop through each file.

4. Get File Versions

  1. Inside the Apply to each action, add a Send an HTTP request to SharePoint action.
  2. Configure the action:
    • Site Address: Select your SharePoint site.
    • Method: GET
    • Uri: _api/web/lists/getbytitle('YourLibraryName')/items(@{items('Apply_to_each')?['ID']})/versions
  3. This action retrieves all versions of each file.

5. Delete Previous Versions

  1. Add another Apply to each action inside the first one to loop through each version.
  2. Set the value from the Send an HTTP request to SharePoint action as the output.
  3. Add a Condition to check if the version is not the current version.
    • Expression: @not(equals(items('Apply_to_each_2')?['IsCurrentVersion'], true))
  4. 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! 😊

No comments:

PowerShell script to delete file versions from the specified SharePoint document library

Managing file versions in SharePoint Online is essential to maintain storage hygiene and performance, especially when versioning is enabled ...