Skip to content
Menu
SharePoint Gems
  • Power Platform
  • Development
    • SharePoint 2013
  • Migration
  • Administration
  • SP Online
  • Contact
  • About
    • Privacy Policy
SharePoint Gems

SharePoint Online: Delete Folders using PnP PowerShell

Posted on April 29, 2020September 29, 2021

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.

Contents hide
1 Requirement
1.1 How to Delete Folder in SharePoint Online?
2 PowerShell Script to Delete SharePoint Folders and SubFolders in SharePoint Online
2.1 PowerShell to delete Folders
2.1.1 Variable Declaration:
2.1.1.1 Delete-AllFolders Function
2.1.1.2 Delete Folders from Sub Folder/Sub sub folders
2.1.1.3 Delete all 3rd level Sub Folders
2.1.1.4 Delete All files from Folder
2.2 Share this:
2.3 Like this:
2.4 Related

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.

Share this:

  • Print
  • Twitter
  • Facebook
  • LinkedIn
  • WhatsApp
  • Telegram
  • Pinterest
  • Reddit

Like this:

Like Loading...

Related

8 thoughts on “SharePoint Online: Delete Folders using PnP PowerShell”

  1. SPAdam says:
    May 7, 2020 at 1:04 PM

    Nicely copied! Shame on you.

    Loading...
    Reply
  2. SPAdam says:
    May 7, 2020 at 1:25 PM

    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?

    Loading...
    Reply
  3. SPAdam says:
    May 7, 2020 at 1:26 PM

    Let me write to the original author..

    Loading...
    Reply
  4. Pingback: Create Approval workflow in MS Flow Step-By-Step - SharePoint Gems
  5. Tienda de CBD y Vape says:
    July 20, 2020 at 9:33 AM

    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?

    Loading...
    Reply
  6. Pingback: Retrieve all users from Site Collection using PowerShell - SharePoint Gems
  7. Pingback: Retrieve SharePoint List Items using CSOM - SharePoint Gems
  8. Pingback: PnP PowerShell to get all Site Collections - SharePoint Gems

Leave a ReplyCancel reply

  • Development
  • Migration
  • Poweer Apps
  • Power Automate
  • Power Platform
  • SharePoint 2013
  • SharePoint Administration
  • SharePoint Online
  • Tips
  • Uncategorized

Best of Computers

Blog Stats

  • 67,509 hits

Subscribe

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,433 other subscribers
Buy Me Coffee
©2025 SharePoint Gems | Powered by WordPress and Superb Themes!
%d