I have a CSV with Site URL and Document library name, I need to delete these document libraries and send them to recycle bin. Can someone please help? I have around 300 site with Document Libraries to delete

Paul, Alok 141 Reputation points
2021-11-28T16:06:06.977+00:00

I have a CSV with Site URL and Document library name, I need to delete these document libraries and send them to recycle bin. Can someone please help? I have around 300 site with Document Libraries to delete

Microsoft 365 and Office | SharePoint | Development
{count} votes

1 answer

Sort by: Most helpful
  1. Echo Du_MSFT 17,316 Reputation points
    2021-11-30T10:29:59.287+00:00

    Hi @Anonymous ,

    Welcome to Q&A Forum!

    Please follow the steps:

    1.In my test, this is my .csv file named LibList.csv

    153589-1.png

    2.Please run the below PowerShell script as an admin on the SharePoint Online Management Shell:

    ##Export the .csv data to SharePoint list  
    
    $Credentials = Get-Credential  
    $Site="https://xxxx.sharepoint.com/sites/yyyy"  
    $ListName = "zzzz"  
    
    Connect-PnPOnline -Url $Site -Credentials $Credentials  
    
    $CustomerData = Import-CSV "C:\temp\LibraryURL.csv"  
    
    foreach ($Record in $CustomerData){   
        Add-PnPListItem -List $ListName -Values @{  
            "Title"= $Record.'Title';  
            "SiteURL"= $Record.'SiteURL';  
            "LibraryURL"= $Record.'LibraryURL';  
            "FullURL"= $Record.'FullURL'          
    }  
    }  
    

    153665-2.png

    153711-3.png

    3.Then, run the below PowerShell script as an admin on the SharePoint Online Management Shell:
    Note: The user must have at least Edit permission for each of the SharePoint sites and Document libraries

    ##Delete Document library  
    
    #Enter user account and password  
    #The user must have at least Edit permission for each of the SharePoint sites and Document libraries  
    $Credentials = Get-Credential  
    
    #Parameter  
    $SiteURL = "https://xxxx.sharepoint.com/sites/yyyy"  
    $ListName = "zzzz"  
    
    #Connect to PnP Online  
    Connect-PnPOnline -Url $SiteURL -Credentials $Credentials  
    
    $ListItems = Get-PnPListItem -List $ListName   
    
    ForEach($Item in $ListItems)  
    {  
        $LibName = $Item["Title"]  
        $Site = $Item["SiteURL"]  
    
        Connect-PnPOnline -Url $Site -Credentials $Credentials  
    
        $Library = Get-PnPList -Identity $LibName -ErrorAction SilentlyContinue  
    
        If($Library)  
        {  
            #sharepoint online powershell to delete library  
            Remove-PnPList -Identity $LibName -Recycle -Force  
            Write-host -f Green "Library '$LibName' Deleted Successfully!"  
        }  
        Else  
        {  
            Write-host -f Yellow "Could not find Library '$LibName'"  
        }  
    }  
    

    153590-4.png

    153655-5.png

    Thanks,
    Echo Du

    ================================================

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.