I have a document library where i have uploaded documents it has 2 major versions and i want to delete the first major version. Is there powershell csom code i can used to do this

1 Answer 1


Connect to your SharePoint Site

# Bind to site 
$Context = New-Object Microsoft.SharePoint.Client.ClientContext("Your URL") 
$Credentials =  Get-Credential -Message "Enter your credentials for SharePoint Online:" 
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName,$Credentials.Password)

# Get Web Object
$Web = $Context.Web

Get Object of the uploaded document

$Upload = $Web.GetFileByServerRelativeUrl($fileUrl)

Get versions of file and delete them

# Get versions and delete
$Version = $Upload.Versions

#[0] is the first version, and its ID is 512

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.