SharePoint Online: Delete Column from List using PowerShell

Requirement: SharePoint Online delete column from list

How to remove a column from SharePoint Online List?
We can delete Columns from lists or libraries when they are no longer needed. To delete a list column, follow these steps:
  • Go to List >> Click on Settings Gear and choose "List Settings" (In Classic experience: Go to List tab >> Under Settings group, click on List Settings button in the ribbon).
    how to delete a column in a sharepoint online list
  • Under the List Settings page, in the Columns section, click on the column title you wish to delete.
  • Scroll down and Click on Delete button.
    sharepoint online list delete column
  • Confirm the deletion prompt by clicking OK.
This deletes SharePoint Online list column.
Please note, When you delete a column in SharePoint, all the data saved in the column is also deleted! We can't restore this column data from recycle bin!


SharePoint Online List: How to Delete a Column in Modern Experience?
To delete a column in modern list, do the following:
  • Navigate to the list or library that you want to delete a column from.
  • Click on the column header for that you want to delete >> Select Column settings >> Edit
    sharepoint online list how to delete column
  • Click on "Delete" button at the bottom of the Edit Column pane.
    how to delete a column in a sharepoint online list

SharePoint Online: Delete List Column using PowerShell
Here is the PowerShell to delete a column from List in SharePoint Online
#Load SharePoint CSOM AssembliesAdd-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"
$ColumnName="Project Code" #Display Name of the column

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 Column to delete
    $Column = $List.Fields.GetByTitle($ColumnName)
    
    #sharepoint online delete list column powershell
    $Column.DeleteObject()
    $Ctx.ExecuteQuery()

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

PnP PowerShell to Remove a Column from SharePoint Online List:
Here is how to remove a column from list using PnP PowerShell in SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName= "Team Projects"
$ColumnName= "ProjectStartDate" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #sharepoint online powershell delete list column
    Remove-PnPField -Identity $ColumnName -List $ListName -Force -ErrorAction Stop
    Write-host "Column '$ColumnName' Removed from the List Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Delete Button missing in Column Settings? Getting "Operation is not valid due to the current state of the object" Error when trying to delete the field using PowerShell? Attempts to set "Sealed" Property also failed? Well, For now I don't find any other ways to delete a sealed column, other than hiding it from the list! How to hide a column from SharePoint Online List?

If you want to remove a site column from SharePoint Online, use: SharePoint Online: Delete Column using PowerShell

5 comments:

  1. This is exactly what I have been searching for!

    ReplyDelete
  2. Great Help! The only thing I found out for "sealed" columns is to add these additional lines to the Column Section and the Update Command:
        #Get the Column to delete
        $Column = $List.Fields.GetByTitle($ColumnName)
    $Column.Hidden = $false
    $Column.ReadOnlyField = $false
    $Column.Sealed = $false
    $Column.Update()
        $Column.DeleteObject()
        $Ctx.ExecuteQuery()

    ReplyDelete
    Replies
    1. I've no luck setting the "Sealed" Property to $False. Tried even updating schema, but it didn't work!

      Delete
  3. Wish there was some direction on how to delete multiple columns! This was great though - thank you

    ReplyDelete
    Replies
    1. Just place them in a Array and use the Remove-PnPField. E.g.
      $ColumnNames = @("Projects", "Department", "Vertical")

      ForEach($Column in $ColumnNames)
      {
      Remove-PnPField -Identity $Column -List $ListName -Force -ErrorAction Stop
      Write-host "Column '$Column' Removed from the List Successfully!" -f Green
      }

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.