SharePoint Online: Delete a Site Column using PowerShell

Requirement: Delete a Site Column in SharePoint Online using PowerShell.

How to delete a site column in SharePoint Online?

Have you ever added a site column to your SharePoint Online environment and later decided that you no longer want it? If you need to delete a site column in SharePoint Online, you can do so using the web browser UI or with PowerShell. This process is relatively simple and only requires a few steps. In this blog post, we will be discussing how to delete site columns in SharePoint Online. We will cover the steps necessary to delete a site column through the web browser and how to delete site columns in SharePoint Online using PowerShell. Let’s get started!

To remove a SharePoint Online site column, follow these steps:

  1. Go to Site Settings by clicking Site settings gear and then “Site settings” from the menu.
  2. Click on the “Site Columns” link from the Web Designer Galleries group >> Select the site column to delete.
  3. Scroll down, click on the “Delete” button, and confirm the prompt to delete.
    sharepoint online powershell delete site column

This eliminates a site column in SharePoint Online. When you remove a site column, any list or library using the site column doesn’t get affected, and the data remains safe!

SharePoint Online PowerShell to Delete Site Column:

In order to delete a site column in SharePoint Online using PowerShell, First, you need to connect to your SharePoint Online site. This will prompt you for your credentials. Once you are connected, Next you need to identify the site column that you want to delete. Finally, you have to call the DeleteObject method.

Here is how to use PowerShell to delete a site column:

#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"
  
#Set Variables for Site URL and Site column Title
$SiteURL= "https://crescent.sharepoint.com/sites/sales"
$ColumnName="Sales Region" #Case sensitive

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

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

    #Check if the site column exists
    $Fields = $Ctx.web.Fields
    $Ctx.Load($Fields)
    $Ctx.ExecuteQuery()
    $FieldNames = $Fields | select -ExpandProperty Title

    #delete site column in sharepoint online
    If($FieldNames.Contains($ColumnName))
    {
        #Delete the site column
        $SiteColumn = $Fields.GetByTitle($ColumnName)
        $Sitecolumn.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-host -f Green "Site Column Deleted Successfully!"
    }
    else
    {
        Write-host -f Yellow "Site Column Doesn't Exists!"
    }
}
Catch {
    write-host -f Red "Error deleting Site Column!" $_.Exception.Message
} 

Once the script has been run, the specified site column will be deleted.

SharePoint Online: PowerShell To Delete All Site Columns in a Group

Deleting site columns is easy with PowerShell. You can quickly delete Site Columns in SharePoint Online to save time and effort when cleaning up a site collection. Let me show you how to delete all site columns in a group using PowerShell:

#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 to Remove All Site Columns of a Group
Function Remove-SiteColumnGroup([String]$SiteURL, [String]$SiteColumnGroup)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Fields)
        $Ctx.ExecuteQuery()

        #Get the site column Group
        $SiteColumns = $Ctx.Web.Fields | where {$_.Group -eq $SiteColumnGroup}
        if($SiteColumns -ne $NULL)
        {
            #Loop Through Each Field
            ForEach($Column in $SiteColumns)
            {
                Write-host "Removing Site Column:" $($Column.InternalName)
                $Column.DeleteObject()
                $Ctx.ExecuteQuery()
            }
            Write-host -f Green "Site Columns Deleted Successfully!"
        }
        Else
        {
            Write-host -f Yellow "Site Column Group '$($SiteColumnGroup)' Does not Exist!"
        }    
    }
    Catch {
    write-host -f Red "Error Removing Site Columns!" $_.Exception.Message
    }
}

#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SiteColumnGroup="Crescent Projects"

#Get Credentials to connect
$Cred= Get-Credential

#Call the function to Import the Site Columns from XML
Remove-SiteColumnGroup $SiteURL $SiteColumnGroup

PnP PowerShell to Delete a Site Column from SharePoint Online site:

Do you have a column in your SharePoint Online site that you want to delete? Deleting a site column in SharePoint Online is a straightforward process with the PnP PowerShell cmdlet Remove-PnPField:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ColumnName= "ProjectStartDate" #Internal Name

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Remove Site column
    Remove-PnPField -Identity $ColumnName -Force -ErrorAction Stop
    Write-host "Site Column '$ColumnName' Removed From the site Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Please note, If the site column is a member of any content type, You can’t delete it until you remove the column from the content type!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

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