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? This blog post will show you how to delete a list view.

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

  1. Login to your SharePoint Online site, and Navigate to the list containing the view you want to delete.
  2. Select the View to delete. Click on Modify this View (Or go to list settings, Click on the respective view title)
  3. On the Edit view page, click on the “Delete” button. Confirm the prompt once to delete the view from the 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 cannot 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
}

After deleting the view, you can verify that it has been deleted by refreshing the list view page and checking that the view is no longer listed. You can also go back to the list settings and check the views list to confirm that the view has been deleted.

PowerShell Script to Delete a List View in SharePoint Online:

The above script can be wrapped into a reusable 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

In SharePoint Online, you can delete a list view that is no longer needed using the Remove-PnPView cmdlet in 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

  • does deleting a view, delete items in that view?

    Reply
  • 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

Your email address will not be published. Required fields are marked *