kwizcom banner advertisement

SharePoint Online: Delete a List using PowerShell

Requirement: Delete a list in SharePoint Online

How to Delete a List in SharePoint Online?
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:
Lets 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.
SharePoint Online: Delete a List using PowerShell SharePoint Online: Delete a List using PowerShell Reviewed by Salaudeen Rajack on October 19, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.