Delete All Versions of a SharePoint File Using PowerShell

There may be instances where you need to remove all previous versions of a file. Well, this is the PowerShell script to do it. This will only work for SharePoint on-premises however.

param(
[string] $UrlToFile
)

$Site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $UrlToFile
$Web = $Site.OpenWeb()
$SPFile = $Web.GetFile($UrlToFile)

Write-Host “Deleting all versions for file $($UrlToFile)…”

# Remove all versions for file…
$SPFile.Versions.DeleteAll()

# Dispose of the web object
$Web.Dispose()

# Dispose of the site object
$Site.Dispose()

Save this off as something rememberable, such as Remove-FileVersions.ps1, and pass it the full URL to the file you wish to remove all versions from, for example:

.\Remove-FileVersions.ps1 “http://mysharepoint.com/sites/foo/Documents/Document_1.docx”

Enjoy!

Advertisement

About Geoff Varosky
Geoff Varosky is a Senior Architect for Insight, based out of Watertown, MA. He has been architecting and developing web based applications his entire career, and has been working with SharePoint for the past 15 years. Geoff is an active member of the SharePoint community, Co-Founder and Co-Organizer of the Boston Area SharePoint Users Group, co-founder for the Boston Office 365 Users Group, co-organizer for SharePoint Saturday Boston and speaks regularly at SharePoint events and user groups.

One Response to Delete All Versions of a SharePoint File Using PowerShell

  1. Pingback: Windows 10 'Ultimate Performance' Mode; SharePoint List Change Notifications; Cloud Computing Market Getting More Competitive

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: