Live training: next batches coming soon. Join the waitlist:Waitlists open:Developer Training →Developer →Copilot Studio Training →Copilot Studio →

How to Delete a Folder in SharePoint Online with PowerShell (PnP)

A project lead asked me to clean up the SPGuides Projects library: last year’s archive folder, an old projects tree, and a pile of empty upload folders. Clicking through them in the browser is slow, and one wrong click takes the wrong folder with it.

A script is faster and repeatable, but deleting folders blindly is risky too. A folder takes every file and subfolder inside it, so I want each delete to land in the recycle bin where I can undo it.

In this tutorial, I’ll show you how I handle delete folder sharepoint powershell jobs with PnP PowerShell: delete one folder, delete a subfolder, clear out only empty folders, handle a missing folder, restore a folder from the recycle bin, and run it in Windows PowerShell 5.1.

Quick answer: Connect with Connect-PnPOnline, then run Remove-PnPFolder -Name "Archive 2025" -Folder "SPGuidesProjects" -Recycle -Force. -Name is the folder to delete, -Folder is its parent’s site-relative URL, -Recycle sends it to the site recycle bin instead of deleting it outright, and -Force skips the confirmation prompt.

What you need before you start

You need the PnP.PowerShell module, your own Entra ID app registration for sign-in, and at least Edit permission on the library. I covered the install and the app registration step by step in my post on creating a folder in SharePoint with PowerShell, so here’s only the short version:

Install-Module PnP.PowerShell -Scope CurrentUser

Connect-PnPOnline -Url "https://spguides.sharepoint.com/sites/SPGuides" -ClientId "<your-app-client-id>" -DeviceLogin -PersistLogin

Replace the client ID with your app’s Application (client) ID. If the install itself gives you trouble, this guide to installing PnP PowerShell for SharePoint Online walks through the usual fixes.

I ran every script below on 10/25/2026 against my /sites/SPGuides site, in PowerShell 7.6.7 with PnP.PowerShell 3.3.0. The library shows as SPGuides Projects, but its URL name is SPGuidesProjects.

How do I delete a folder with Remove-PnPFolder?

Remove-PnPFolder takes the folder name and its parent path as two separate values. That trips people up, because you don’t pass the full path of the folder you’re deleting:

ParameterRequiredWhat it does
-NameYesThe name of the folder to delete, for example "Archive 2025"
-FolderYesThe parent folder’s site-relative URL: "SPGuidesProjects" or "SPGuidesProjects/Old Projects"
-RecycleNoMoves the folder to the recycle bin instead of deleting it directly
-ForceNoSkips the “are you sure” confirmation prompt

Here’s the script I ran on the Archive 2025 folder. It counts the files first, deletes the folder to the recycle bin, then checks whether it’s still there:

$folder = Get-PnPFolder -Url "SPGuidesProjects/Archive 2025" -Includes ItemCount
"Deleting $($folder.ServerRelativeUrl) ($($folder.ItemCount) files inside)"

# -Recycle sends the folder and everything in it to the site recycle bin; -Force skips the prompt
Remove-PnPFolder -Name "Archive 2025" -Folder "SPGuidesProjects" -Recycle -Force

$stillThere = Get-PnPFolder -Url "SPGuidesProjects/Archive 2025" -ErrorAction SilentlyContinue
"Archive 2025 still exists: $([bool]$stillThere)"
Remove-PnPFolder with -Recycle deleting the Archive 2025 folder and its 3 files in PowerShell 7

With -Recycle, the cmdlet returns a RecycleBinItemId, the ID of the new recycle bin entry. The final check confirms the folder is gone:

Deleting /sites/SPGuides/SPGuidesProjects/Archive 2025 (3 files inside)

RecycleBinItemId
----------------
c928b896-ec82-4f73-a7d3-4f0cf5a272bf
Archive 2025 still exists: False

The -Folder value is the library’s URL name, not its display name. The default Documents library is Shared Documents, for example. To look up URL names, list all lists and libraries with PnP PowerShell.

What happens without -Recycle?

According to the PnP documentation, leaving out -Recycle deletes the folder directly instead of moving it to the recycle bin, so you can’t restore it from there. I never run it that way on client sites, and I didn’t run it for this post either.

Leaving out -Force is harmless: PowerShell just asks you to confirm each delete. I keep the prompt while I test a script and add -Force once the paths are right.

Where does the deleted folder go?

With -Recycle, the folder and everything in it land in the site recycle bin, exactly as if you’d deleted it in the browser. Open the site, go to Site contents, and select Recycle bin.

Here’s my site recycle bin after the test runs. Each entry shows the original location and who deleted it, and Q1 2027 Planning appears twice because I deleted it in two separate runs:

SharePoint site recycle bin listing the Archive 2025, 2024 and Batch folders deleted with Remove-PnPFolder

Microsoft keeps deleted items for 93 days from the original delete. That window covers both the site recycle bin and the second-stage site collection recycle bin. My SharePoint recycle bin tutorial explains both stages.

Delete a subfolder and everything inside it

To delete a folder deeper in the tree, put the full parent path in -Folder. My Old Projects folder had a 2024 subfolder with Kickoff and Status Reports inside it. Get-PnPFolderItem lists what’s about to go before the delete runs:

# -Folder is the parent path; -Name is the folder to delete (its subfolders go with it)
Get-PnPFolderItem -FolderSiteRelativeUrl "SPGuidesProjects/Old Projects/2024" -ItemType Folder |
    Select-Object Name

Remove-PnPFolder -Name "2024" -Folder "SPGuidesProjects/Old Projects" -Recycle -Force | Out-Null
"Deleted: SPGuidesProjects/Old Projects/2024 and its subfolders"
Get-PnPFolderItem listing the Kickoff and Status Reports subfolders before Remove-PnPFolder deletes the 2024 folder

Both subfolders went with 2024; there’s no separate recursive switch. If you only want to relocate a branch, not delete it, move the folder in SharePoint Online instead.

Delete only empty folders

Upload jobs and migrations leave behind empty folders. This loop checks each subfolder’s ItemCount and deletes only the ones with nothing in them:

$parent = "SPGuidesProjects/Temp Uploads"

$folders = Get-PnPFolderItem -FolderSiteRelativeUrl $parent -ItemType Folder
foreach ($f in $folders) {
    if ($f.ItemCount -eq 0) {
        Remove-PnPFolder -Name $f.Name -Folder $parent -Recycle -Force | Out-Null
        "Deleted empty folder: $($f.Name)"
    } else {
        "Kept: $($f.Name) ($($f.ItemCount) item)"
    }
}

My Temp Uploads folder had Batch 01 through Batch 05, and only Batch 05 held a file (an upload log). The script deleted the four empty ones and kept the fifth:

Deleted empty folder: Batch 01
Deleted empty folder: Batch 02
Deleted empty folder: Batch 03
Deleted empty folder: Batch 04
Kept: Batch 05 (1 item)
PowerShell loop deleting empty Batch folders in Temp Uploads and keeping Batch 05 with one item

ItemCount counts subfolders too, so a folder that holds only an empty subfolder is kept. Run the loop again to catch the next level. If empty folders keep coming back, revisit your SharePoint folder structure.

Restore a deleted folder with PnP PowerShell

When someone deletes the wrong folder, you can bring it back without opening the browser. Get-PnPRecycleBinItem reads the recycle bin, and Restore-PnPRecycleBinItem puts the item back in its original location:

$item = Get-PnPRecycleBinItem -FirstStage |
    Where-Object { $_.LeafName -eq "Archive 2025" -and $_.ItemType -eq "Folder" }
$item | Select-Object LeafName, DirName, ItemType, DeletedByName

$item | Restore-PnPRecycleBinItem -Force
$restored = Get-PnPFolder -Url "SPGuidesProjects/Archive 2025" -ErrorAction SilentlyContinue
"Archive 2025 restored: $([bool]$restored)"
Get-PnPRecycleBinItem finding the Archive 2025 folder and Restore-PnPRecycleBinItem restoring it

The output showed Archive 2025 from sites/SPGuides/SPGuidesProjects with ItemType Folder, then Archive 2025 restored: True. Filter on ItemType, because a file and a folder can share a name.

Two catches. Get-PnPRecycleBinItem needs Site Collection Administrator rights, so a site member gets nothing back. And restore the folder entry itself: restoring a single file from a deleted folder recreates the folder with only that file in it.

Delete a folder in Windows PowerShell 5.1

PnP.PowerShell 3.x needs PowerShell 7.4 or later. On a server that only has Windows PowerShell 5.1, the last version that loads is PnP.PowerShell 1.12.0, and its -DeviceLogin also needs -Tenant:

Install-Module PnP.PowerShell -RequiredVersion 1.12.0 -Scope CurrentUser
Connect-PnPOnline -Url "https://spguides.sharepoint.com/sites/SPGuides" -ClientId "<your-app-client-id>" -Tenant "spguides.onmicrosoft.com" -DeviceLogin

Remove-PnPFolder takes the same parameters in 1.12.0. This is the script I ran there:

"PowerShell $($PSVersionTable.PSVersion) / PnP.PowerShell $((Get-Module PnP.PowerShell).Version)"

Remove-PnPFolder -Name "Q1 2027 Planning" -Folder "SPGuidesProjects" -Recycle -Force

$check = Get-PnPFolder -Url "SPGuidesProjects/Q1 2027 Planning" -ErrorAction SilentlyContinue
"Q1 2027 Planning still exists: $([bool]$check)"
Windows PowerShell 5.1 with PnP.PowerShell 1.12.0 deleting the Q1 2027 Planning folder with Remove-PnPFolder

It printed PowerShell 5.1.26100.9444 with PnP.PowerShell 1.12.0, a RecycleBinItemId, and still exists: False. If 5.1 says the cmdlet isn’t recognized, see this fix for Connect-PnPOnline is not recognized.

What to check before you delete a folder

  • What’s inside: run Get-PnPFolderItem with -Recursive first. A folder delete takes every file and subfolder with it.
  • Retention labels and policies: retention settings don’t apply to folders themselves, but they do apply to the files inside. A file under a retention policy can be deleted, and SharePoint keeps a copy in the hidden Preservation Hold library.
  • Records: a file with a locked record label or a regulatory record label can’t be deleted, so the folder delete fails with an error.
  • Flows and links: anything that writes to the folder path, such as a Power Automate flow, breaks once the folder is gone.

If you only want the files gone and the folder kept, delete the files instead. This flow shows how to delete all files in a SharePoint folder with Power Automate.

Common errors and how to fix them

The one I see most is File Not Found. It means the folder name or the parent path is wrong. This test deleted a folder that doesn’t exist, with -ErrorAction Stop so the catch block runs:

try {
    Remove-PnPFolder -Name "Archive 2026" -Folder "SPGuidesProjects" -Recycle -Force -ErrorAction Stop
} catch {
    "Remove failed: $($_.Exception.Message)"
}
Remove-PnPFolder returning File Not Found for the missing Archive 2026 folder
  • “File Not Found.”: check the folder’s spelling and that -Folder is the parent path with the library’s URL name, not its display name.
  • Access denied: your account needs at least Edit permission on the library to delete folders.
  • Get-PnPRecycleBinItem returns nothing: you need to be a Site Collection Administrator, or the item already moved to the second stage (use -SecondStage).
  • Delete blocked by a label: a file inside has a record label. Ask your compliance admin before you touch it.

For more ready-made admin scripts, browse my PnP PowerShell scripts for SharePoint developers or the SharePoint tutorials hub.

If you’re cleaning up more than folders, these PnP PowerShell tutorials cover the next jobs:

Frequently Asked Questions

Does Remove-PnPFolder delete the files inside the folder?

Yes. It deletes the folder with every file and subfolder in it. In my test, the Archive 2025 folder and its three files went to the site recycle bin together.

How do I delete a SharePoint folder permanently with PowerShell?

Run Remove-PnPFolder without -Recycle. The Remove-PnPFolder reference says the folder is then deleted directly, so it can’t be restored from the recycle bin. Use -Recycle unless you’re sure.

How long can I restore a deleted SharePoint folder?

For 93 days from the original delete, across the site recycle bin and the second-stage site collection recycle bin. Microsoft’s recycle bin restore guide covers both stages.

Can I delete a folder with the SharePoint Online Management Shell?

No. The SharePoint Online Management Shell has no folder cmdlets, so folder work needs PnP PowerShell’s Remove-PnPFolder.

How do I delete all empty folders in a SharePoint library?

Get the subfolders with Get-PnPFolderItem -ItemType Folder, then call Remove-PnPFolder -Recycle -Force only for folders whose ItemCount is 0. Rerun it to catch folders that become empty.