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 delete any list or library available in 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 to remove irrelevant lists and libraries to keep the collaboration space clean. Anyway, whatever may be the reason, To delete a list in SharePoint Online:
  • 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 "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
Now, lets delete list in SharePoint Online using PowerShell

Delete 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)

    #Delete the 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 SharePoint Online list using PowerShell.

Delete SharePoint Online List using PowerShell:
Let's delete a list and send 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)
        $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://crescenttech.sharepoint.com/sites/projects"
$ListName = "Projecs v1"

#Call the function
Delete-SPOList -SiteURL $SiteURL -ListName $ListName
This deletes list SharePoint Online with PowerShell.

Delete SharePoint Online List using PnP PowerShell
#Config Variables
$SiteURL = "https://crescenttech.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 "-Recycle" switch to send the list or library to recycle bin!
SharePoint Online: Delete a List using PowerShell SharePoint Online: Delete a List using PowerShell Reviewed by Salaudeen Rajack on October 19, 2016 Rating: 5

1 comment:

  1. This worked perfectly for my list that had "delete" disabled. Thanks!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.