Any PowerShell Script for SharePoint Online that can delete all Survey Lists in a site collection?


You can use the following cmdlets:

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

#Variables for Processing
$SiteUrl = "https://<tenant>.sharepoint.com/Sites/<site>"
$Password ="<password>"
$ListTemplate = "Survey"

#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

Try {
    #Function to Get all lists from the web
    Function Get-SPOList($Web)
        $listsToDelete = @()
        #Get All Lists from the web
        $Lists = $Web.Lists

        #Get all lists from the web  
        foreach($List in $Lists)
            #Get the List
            if($List.BaseType -eq $ListTemplate){
                $listsToDelete +=$Lists.GetByTitle($List.Title)
                Write-Host "List:" $List.Title " will be Deleted!" -ForegroundColor Green 
        foreach($listToDelete in $listsToDelete) {
            $listToDelete.DeleteObject() #permanently delete
            #$listToDelete.Recycle() #Delete the List - Send to Recycle bin
        Write-Host "The Survey lists are Deleted Successfully!" -ForegroundColor Green 

    #Function to get all webs from given URL
    Function Get-SPOWeb($WebURL)
        #Set up the context
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
        $Context.Credentials = $Credentials

        $Web = $context.Web
        #Get all immediate subsites of the site

        #Call the function to Get Lists of the web
        Write-host "Processing Web :"$Web.URL
        Get-SPOList $Web

        #Iterate through each subsite in the current web
        foreach ($Subweb in $web.Webs)
            #Call the function recursively to process all subsites underneaththe current web

    #Call the function to get all sites
    Get-SPOWeb $SiteUrl
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red

How to get all lists in a SPO site collection using PowerShell

How to delete a SPO list using PowerShell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.