SharePoint Online: Delete a List using PowerShell

Requirement: Delete a list in SharePoint Online

How to Delete a List in SharePoint Online?

You may want to delete any list or library available on the SharePoint Online site when they are not needed anymore. Or you may want to delete the document library to free up some storage space or remove irrelevant lists and libraries to keep the collaboration space clean. Anyway, whatever may be the reason, To delete a list in SharePoint Online, do the following:

  • Click on Site Settings gear >> Select Site contents from the menu.
  • On the site contents page, Hover over the list that you want to delete and then click the drop-down menu icon (…)
  • On the menu that appears, click on the “Remove” link and then confirm the prompt to send the list to the Recycle Bin. 
  • Alternatively, you may remove a list in SharePoint Online by going to list settings >> Click on “Delete this list” under Permissions and Management group.
    delete list sharepoint online powershell

Similarly, in modern SharePoint Online sites, you can delete a list in the site contents page from the respective list context menu:

sharepoint online how to delete a list

Keep in mind: when you delete a list, it is not permanently deleted and can be recovered within 93 days. Alright, now, let’s delete a list in SharePoint Online using PowerShell.

Delete List in SharePoint Online using PowerShell:

What if you want to delete a list in SharePoint Online without using the browser interface? Because You may have to remove specific hidden lists or automate list deletion in the SharePoint environment at times. Let’s go over how to delete a list in SharePoint Online using PowerShell. Here is the SharePoint Online PowerShell to delete list.

#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 List Name
$SiteURL= "https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"

#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

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

    #powershell delete sharepoint online list
    $List.DeleteObject()
    $Ctx.ExecuteQuery()
        
    Write-host -f Green "List Deleted Successfully!" 
}
Catch {
    write-host -f Red "Error deleting list!" $_.Exception.Message
}

This permanently deletes the SharePoint Online list using PowerShell. You can also use this PowerShell to delete a SharePoint Online document library!

Delete SharePoint Online List using PowerShell:

What if you don’t find the “Delete” option for the list? Well, let’s turn ON allow deletion flag and then delete a list by sending it to recycle bin in SharePoint Online with PowerShell-CSOM.

#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"

#Custom function to delete a SharePoint Online list using powershell
Function Delete-SPOList
{
    param
    (
        [string]$SiteURL  = $(throw "Please Enter the Site URL!"),
        [string]$ListName = $(throw "Please Enter the List Name to Delete!")
    )
    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 and set "AllowDeletion" flag to True
        $List=$Ctx.Web.Lists.GetByTitle($ListName)
        
        #sharepoint online powershell allow deletion
        $List.AllowDeletion = $True
        $List.Update()
        $Ctx.ExecuteQuery()
             
        #Delete the List - Send to Recycle bin
        $List.Recycle() | Out-Null  
        $Ctx.ExecuteQuery()
 
        Write-Host "List: '$ListName' has been Deleted Successfully!" -ForegroundColor Green  
    }
    Catch {
        write-host -f Red "Error Deleting List!" $_.Exception.Message
    }
}

#Variables
$SiteURL="https://Crescent.sharepoint.com/sites/projects"
$ListName = "Projecs v1"

#Call the function
Delete-SPOList -SiteURL $SiteURL -ListName $ListName

This deletes the list SharePoint Online with PowerShell.

Delete SharePoint Online List using PnP PowerShell

Here is the PnP PowerShell to delete SharePoint Online list:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName ="Project Docs"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
        
#Check if List exists
If(Get-PnPList -Identity $ListName)
{
    #sharepoint online powershell remove list
    Remove-PnPList -Identity $ListName -Force
    Write-host -f Green "List '$ListName' Deleted Successfully!"
}
Else
{
    Write-host -f Yellow "Could not find List '$ListName'"
}

The Remove-PnPList deletes the list permanently. Use the “-Recycle” switch to send the list or library to recycle bin!

A similar script works when you want to delete a document library in SharePoint Online: Delete SharePoint Online Document Library using PowerShell

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 a List using PowerShell

Leave a Reply

Your email address will not be published.