Hello SharePointers,
In earlier posts, we saw How to Create Communication site and Team site using PowerShell. Today We will see How to Delete Folders using PnP PowerShell in SharePoint Online.

Requirement

Remove folders and Subfolders from SharePoint online Library using PowerShell.

How to Delete Folder in SharePoint Online?

To drop a folder in SharePoint Online, we just need to navigate to a particular folder in SharePoint Online Library. Select a “Folder” and click on the “Delete” button/icon.

SharePoint Online : Delete Folders using PnP PowerShell
Delete Folders from SharePoint Online Library

In this way, we can remove Folders manually.

What if the folders have some/couple of sub-folders or files. At that time it will throw an error. Thus before deleting the parent folder, we need to delete its sub-folders.

Delete SharePoint Folders and SubFolders using PowerShell
*Delete SharePoint Folders and Sub-Folders

So as per the above error image, we need to delete all subfolders and files manually before deleting the parent folder.

PowerShell Script to Delete SharePoint Folders and SubFolders in SharePoint Online

In this segment we will achieve the same functionality as above. This will delete all folders automatically.

Lets not waste much time and let us see the PowerShell script for the same.

PowerShell to delete Folders

Below is the script which is used to remove folders from SharePoint library.

Variable Declaration:

$SiteUrl represents the URL of the site from where you want to remove folders.

$ListName represents the name of the library.

$SiteURL = "https://<yourtenant>.sharepoint.com/teams/<your site>"
#Connect to the Site
Connect-PnPOnline -URL $SiteURL -UseWebLogin
$ListName = "/Estimate Documents"
#call Delete-AllFolders Function
Delete-AllFolders $ListName
Delete-AllFolders Function

This function is for delete all parent folders. To delete sub Folders we have written one folder. Let us see a code block for the same.

Function Delete-AllFolders($Library)
{
$LibFolders= Get-PnPFolderItem -FolderSiteRelativeUrl $Library -ItemType Folder
Foreach($folder in $LibFolders)
{
    #Exclude "Forms" and Hidden folders
        If( ($folder.Name -ne "Forms") -and (-Not($folder.Name.StartsWith("_"))))
        {
            #Call the function to delete Subfolders
            Delete-AllSubFolders -Foler "/Estimate Documets/"$folder.Name
            Remove-PnPFolder -Name $folder.Name -Folder "/Estimate Documents" -Force -Recycle
            Write-Host -f Green $folder.Name "Deleted"
        }
}
}
Delete Folders from Sub Folder/Sub sub folders

The Below code represents How to Delete all folders from Sub folder. for this we need to call a function recursively.

Function Delete-AllSubFolders($folder)
 {
 $relativeUrl="/Estimate Documents/$folder"
 $SubFolders= Get-PnPFolderItem -FolderSiteRelativeUrl  $relativeUrl -ItemType Folder
Foreach($SubFolder in $SubFolders)
{
    #Exclude "Forms" and Hidden folders
        If( ($Subfolder.Name -ne "Forms") -and (-Not($Subfolder.Name.StartsWith("_"))))
        {
            #Call the function recursively
            Delete-AllFilesFromFolder -Folder $Subfolder
           Remove-PnPFolder -Name $Subfolder.Name -Folder $relativeUrl -Force -Recycle
            Write-Host -f Green $Subfolder.Name "Deleted"
        }
}
}
Delete all 3rd level Sub Folders

Below is the code to delete all 3rd levels sub folders.

Function Delete-AllSubSubFromFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    #Get the site relative path of the Folder
    $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($web.ServerRelativeUrl,"")
    $FolderSiteRelativeURL= $FolderSiteRelativeURL.Replace("/teams/MSUS-Project-Estimating","")
 
    #Get All files in the folder
    $SubFolers = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
     
    #Delete all files
    ForEach ($folderin $SubFolers )
    {
        Write-Host ("Deleting File: '{0}' at '{1}'" -f $File.Name, $folder.ServerRelativeURL)
         Delete-AllFilesFromFolder -Folder $File
        #Delete Item
        #Remove-PnPFolder -ServerRelativeUrl $folder.ServerRelativeURL -Force -Recycle
        Remove-PnPFolder -Name $folder.Name -Folder $FolderSiteRelativeURL -Force -Recycle
    }
 }
Delete All files from Folder

In below code snipset we will see how to delete all files from a folder using pnp PowerShell.

Function Delete-AllFilesFromFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    #Get the site relative path of the Folder
    $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($web.ServerRelativeUrl,"")
    $FolderSiteRelativeURL= $FolderSiteRelativeURL.Replace("/teams/MSUS-Project-Estimating","")
 
    #Get All files in the folder
    $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
     
    #Delete all files
    ForEach ($File in $Files)
    {
        Write-Host ("Deleting File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)
         Delete-AllFilesFromFolder -Folder $File
        #Delete Item
        #Remove-PnPFolder -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle
        Remove-PnPFolder -Name $File.Name -Folder $FolderSiteRelativeURL -Force -Recycle
    }
 }

Below is the full code snip set to remove or delete all files from SharePoint Online document Library.

#Parameters
$SiteURL = "https://<your tenant>.sharepoint.com/teams/<your site name>
$ListName = "Estimate Documents"
 
#Connect to the Site
Connect-PnPOnline -URL $SiteURL -UseWebLogin
 
#Get the web & document Library
$Web = Get-PnPWeb
$List = Get-PnPList -Identity $ListName
 
#Function to delete all items in a folder - and sub-folders recursively
Function Delete-AllFilesFromFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    #Get the site relative path of the Folder
    $FolderSiteRelativeURL = $Folder.ServerRelativeUrl.Replace($web.ServerRelativeUrl,"")
 
    #Get All files in the folder
    $Files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType File
     
    #Delete all files
    ForEach ($File in $Files)
    {
        Write-Host ("Deleting File: '{0}' at '{1}'" -f $File.Name, $File.ServerRelativeURL)
         
        #Delete Item
        Remove-PnPFile -ServerRelativeUrl $File.ServerRelativeURL -Force -Recycle
    }
 
    #Process all Sub-Folders
    $SubFolders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeURL -ItemType Folder
    Foreach($Folder in $SubFolders)
    {
        #Exclude "Forms" and Hidden folders
        If( ($Folder.Name -ne "Forms") -and (-Not($Folder.Name.StartsWith("_"))))
        {
            #Call the function recursively
            Delete-AllFilesFromFolder -Folder $Folder
        }
    }
}
 
#Get the Root Folder of the Document Library and call the function
Delete-AllFilesFromFolder -Folder $List.RootFolder


This is the whole code about to delete all files and folders from Library in SharePoint Online using PowerShell. Find more details on SharePoint Diary.

For all such articles kindly visit SharePoint gems and also subscribe us.

8 thoughts on “SharePoint Online: Delete Folders using PnP PowerShell”
  1. You had copied these scripts from SharePointDiary.com and posted with slight changes as if they are your own work.. not just this one but many! How wonderful? Do you know without giving link to the original author, your posts/website will be deleted under DMCA?

  2. I am the owner of JustCBD Store company (justcbdstore.com) and I’m presently trying to develop my wholesale side of company. I really hope that anybody at targetdomain share some guidance 🙂 I thought that the most suitable way to accomplish this would be to talk to vape companies and cbd retailers. I was hoping if anybody could suggest a reliable web site where I can get Vape Shop Business Leads I am already examining creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. Not sure which one would be the most suitable option and would appreciate any assistance on this. Or would it be easier for me to scrape my own leads? Suggestions?

  3. I’m the business owner of JustCBD Store brand (justcbdstore.com) and I am currently planning to expand my wholesale side of company. I am hoping someone at targetdomain share some guidance ! I considered that the best way to accomplish this would be to reach out to vape stores and cbd retail stores. I was really hoping if someone could recommend a trusted web-site where I can get CBD Shops Business Data I am currently reviewing creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. Unsure which one would be the best option and would appreciate any support on this. Or would it be easier for me to scrape my own leads? Ideas?

Leave a Reply