SharePoint Online: Delete List View using PowerShell

Requirement: Delete a view in SharePoint Online

How to Delete a View in SharePoint Online?

Are you looking for a way to delete a list view that is no longer needed in SharePoint Online? In this blog post, we will show you how to delete a list view.

To delete a list view in SharePoint Online, follow these steps:

  • Login to your SharePoint Online site, Navigate to the list containing the view you want to delete.
  • Select the View to delete. Click on Modify this View (Or go to list settings, Click on the respective view title)
  • On Edit view page, Click on “Delete” button.Confirm the prompt once to delete the view from SharePoint Online list or library.
    How to delete a view in sharepoint online

Please note, Except for the default view, You can delete any existing views of the SharePoint Online lists and libraries. If you want to delete a default view, you must first set a different view as the default and delete the former one!

SharePoint Online: Delete Custom View using PowerShell

If you are experiencing issues with the view and could not delete it from the web user interface, You can use PowerShell to delete a list view in SharePoint Online. Here is the PowerShell to Delete a view in SharePoint Online:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteURL="https://crescent.sharepoint.com"
$ListName= "Projects"
$ViewName="Active Projects"

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the List
    $List = $Ctx.Web.Lists.GetByTitle($ListName)

    #Get the view to delete
    $View = $List.Views.getByTitle($ViewName)
    
    #Delete the list view
    $View.DeleteObject()
    $Ctx.ExecuteQuery()

    Write-host "View '$ViewName' deleted Successfully!" -ForegroundColor Green
 }
Catch {
    write-host -f Red "Error Deleting List View!" $_.Exception.Message
}

PowerShell Script to Delete a List View in SharePoint Online:

The above script can be wrapped into a re-usable function. Here is how:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
Function Remove-ListView()
{
    param(
        [Parameter(Mandatory=$true)][string]$SiteURL,
        [Parameter(Mandatory=$false)][System.Management.Automation.PSCredential] $Cred,
        [Parameter(Mandatory=$true)][string]$ListName,
        [Parameter(Mandatory=$true)][string]$ViewName
    )

    Try {
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Get the view to delete
        $View = $List.Views.getByTitle($ViewName)
    
        #Delete the list view
        $View.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-host "View '$ViewName' deleted Successfully!" -ForegroundColor Green
     }
    Catch {
        write-host -f Red "Error Deleting List View!" $_.Exception.Message
    }
}

#Call the function to delete list view
Remove-ListView -SiteURL "https://crescent.sharepoint.com" -Cred (Get-Credential) -ListName "Projects" -ViewName "Active Projects"

SharePoint Online: Delete a view using PnP PowerShell

To delete a list view, use this PnP PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName= "Projects"
$ViewName= "Active Projects"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred

    #Remove List View
    Remove-PnPView -List $ListName -Identity $ViewName -Force -ErrorAction Stop
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

2 thoughts on “SharePoint Online: Delete List View using PowerShell

  • Hi, great script, how could i loop this to get a list of sites from a csv file
    I currently have a view i need to delete from 700 different sites. The list name is the same and view name is the same, just the site url that is different
    Any help would be fantastic
    Glen

    Reply

Leave a Reply