Share via

PowerShell issue

sharepointuser-5603 160 Reputation points
2024-11-15T02:14:41.25+00:00

Hi guys,

For bulk deleting "Document libraries", how we can achieve followings:

  • PowerShell codes to delete all the document libraries at once.
  • Delete document libraries based on CSV file with library name.

Thanks.

Microsoft 365 and Office | SharePoint | Development
0 comments No comments

Answer accepted by question author

Emily Du-MSFT 51,986 Reputation points Microsoft External Staff
2024-11-15T03:05:19.9066667+00:00

1.Delete all document libraries at once.

$SiteURL = "https://tenant.sharepoint.com/sites/test"
Connect-PnPOnline -Url $SiteURL -Interactive
$ExcludedLibraries = @("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates", "Site Collection Documents","Site Collection Images",
                        "Reporting Templates","Solution Gallery", "Style Library", "Web Part Gallery","Site Assets", "wfpub", "Site Pages", "Images")
$Libraries = Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false -and $ExcludedLibraries -notcontains $_.Title}
ForEach($Library in $Libraries)
{
    #Delete document library in sharepoint online using powershell
    Remove-PnPList -Identity $Library -Recycle -Force
    Write-host -f Green "Library '$($Library.Title)' Deleted Successfully!"
}

2.Delete document libraries based on CSV file with library names.

Create a CSV file then run following PowerShell.

User's image

$SiteURL = "https://tenant.sharepoint.com/sites/test"
$CSVFilePath = "C:\Libraries.csv"
Connect-PnPOnline -Url $SiteURL -Interactive
$CSVFile = Import-CSV $CSVFilePath 
ForEach($Row in $CSVFile)
{
        Remove-PnPList -Identity $Row.LibraryName -Force -recycle
        Write-host -f Green "Document Library '$($Row.LibraryName)' Deleted Successfully!"
}

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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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