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

How to Delete a OneDrive Folder with PowerShell (PnP, Tested)

Someone’s OneDrive is full of old export folders, and clicking through My files one folder at a time won’t cut it. A script can clean it up in seconds and keep every delete reversible.

In this tutorial, we’ll walk you through the powershell delete onedrive folder job with PnP PowerShell: connecting to a OneDrive, deleting a folder with its files, nested and bulk deletes, restoring a folder, and what admins run to delete a whole OneDrive.

Quick answer: Connect to the OneDrive URL with Connect-PnPOnline (add -Tenant), then run Remove-PnPFolder -Name "SPGDemo Old Exports" -Folder "Documents" -Recycle -Force. Documents is the OneDrive library, -Recycle sends the folder to the OneDrive recycle bin, and -Force skips the prompt.

Remove-PnPFolder -Name "SPGDemo Old Exports" -Folder "Documents" -Recycle -Force

What You Need Before You Start

  • PowerShell 7.4 or later with PnP.PowerShell 3.x and your own Entra ID app registration. If the module isn’t there yet, follow our guide to installing PnP PowerShell for SharePoint Online.
  • Access to the OneDrive. Your own works out of the box; for someone else’s, you need to be a site collection admin of that OneDrive.
  • The OneDrive URL. It’s built from the user name: admin@spguides.onmicrosoft.com becomes https://spguides-my.sharepoint.com/personal/admin_spguides_onmicrosoft_com.

We ran every script on 11/01/2026 in PowerShell 7 with PnP.PowerShell 3.3.0, against our own OneDrive and only on folders named SPGDemo.

Step 1: Connect to the OneDrive with PnP PowerShell

A OneDrive is a SharePoint site under the -my host, so you connect to it like any other site. If you’re new to how it’s organized, our overview of OneDrive for Business and its features covers the basics.

Install-Module PnP.PowerShell -Scope CurrentUser

$OneDriveUrl = "https://spguides-my.sharepoint.com/personal/admin_spguides_onmicrosoft_com"
$ClientId    = "<your-app-client-id>"
$Tenant      = "spguides.onmicrosoft.com"

Connect-PnPOnline -Url $OneDriveUrl -ClientId $ClientId -Tenant $Tenant -DeviceLogin -PersistLogin

"Connected to: $((Get-PnPWeb).ServerRelativeUrl)"

# "Documents" is the OneDrive library (My files in the browser)
Get-PnPFolderItem -FolderSiteRelativeUrl "Documents" -ItemType Folder |
    Where-Object Name -like "SPGDemo*" | Select-Object Name, ItemCount
Connect-PnPOnline to a OneDrive URL and Get-PnPFolderItem listing the SPGDemo folders in Documents

The connection printed the /personal/ path, and the folder list showed our five demo folders. SPGDemo Old Exports has an ItemCount of 3: two subfolders and a log file.

Important: keep -Tenant on the connect line. Without it, PnP tries to work out the tenant from the -my host name and the sign-in fails (we show the error further down).

Step 2: Delete a OneDrive Folder and Everything in It

Remove-PnPFolder takes the folder name and its parent path separately. In a OneDrive, the parent for a top-level folder is always Documents, even though the browser calls it My files. This script counts the files first, then deletes:

$files = Get-PnPFolderItem -FolderSiteRelativeUrl "Documents/SPGDemo Old Exports" -ItemType File -Recursive
"Files inside SPGDemo Old Exports: $($files.Count)"

# -Folder = parent path, -Name = folder to delete; -Recycle keeps it restorable, -Force skips the prompt
Remove-PnPFolder -Name "SPGDemo Old Exports" -Folder "Documents" -Recycle -Force

$left = Get-PnPFolder -Url "Documents/SPGDemo Old Exports" -ErrorAction SilentlyContinue
"SPGDemo Old Exports still exists: $([bool]$left)"
Remove-PnPFolder with -Recycle deleting the SPGDemo Old Exports OneDrive folder and its 4 files

The folder held 4 files across its subfolders. With -Recycle, the cmdlet returns the new RecycleBinItemId, and the final check confirms the folder is gone:

Files inside SPGDemo Old Exports: 4

RecycleBinItemId
----------------
c0ddf4e4-0dc0-4b5e-ad67-80377b756db6
SPGDemo Old Exports still exists: False

What does “force delete” mean here?

-Force only skips the “are you sure” prompt. The part that decides whether you can undo it is -Recycle: per the PnP docs, leaving it out deletes the folder outright instead of moving it to the recycle bin. We didn’t run it that way, and we don’t recommend it on a user’s files.

Step 3: Delete a Nested Folder by Its Parent Path

For a folder deeper in the tree, put the full path of its parent in -Folder. Our SPGDemo Reports folder has an Archive folder with two subfolders, and we only wanted Old Drafts gone. The same parameters work on any site, as our guide to deleting a SharePoint Online site with PowerShell shows for whole sites.

$parent = "Documents/SPGDemo Reports/Archive"
Get-PnPFolderItem -FolderSiteRelativeUrl $parent -ItemType Folder | Select-Object Name, ItemCount

Remove-PnPFolder -Name "Old Drafts" -Folder $parent -Recycle -Force | Out-Null

"After the delete:"
Get-PnPFolderItem -FolderSiteRelativeUrl $parent -ItemType Folder | Select-Object Name, ItemCount
Remove-PnPFolder deleting the nested Old Drafts folder and keeping the Final folder in OneDrive

Before the delete, Archive held Final (0 items) and Old Drafts (1 item). Afterward only Final was left, so the sibling folder stayed untouched.

Step 4: Delete Several OneDrive Folders at Once

When a sync job or an export tool leaves a pile of folders behind, filter them by name and loop. We matched everything that starts with SPGDemo Temp:

$targets = Get-PnPFolderItem -FolderSiteRelativeUrl "Documents" -ItemType Folder |
    Where-Object Name -like "SPGDemo Temp*"

foreach ($f in $targets) {
    Remove-PnPFolder -Name $f.Name -Folder "Documents" -Recycle -Force | Out-Null
    "Recycled: Documents/$($f.Name)"
}
"Done: $($targets.Count) folders moved to the OneDrive recycle bin"
Recycled: Documents/SPGDemo Temp 01
Recycled: Documents/SPGDemo Temp 02
Recycled: Documents/SPGDemo Temp 03
Done: 3 folders moved to the OneDrive recycle bin
PowerShell loop recycling the SPGDemo Temp 01 to 03 OneDrive folders with Remove-PnPFolder

All three folders went to the recycle bin. Pro Tip: run the loop once with the Remove-PnPFolder line commented out, so you see exactly which names your filter catches.

Step 5: Restore a Folder from the OneDrive Recycle Bin

Deleted the wrong folder? Because we used -Recycle, it’s sitting in the OneDrive recycle bin, which works like the SharePoint recycle bin. Get-PnPRecycleBinItem finds it and Restore-PnPRecycleBinItem puts it back:

$item = Get-PnPRecycleBinItem -FirstStage |
    Where-Object { $_.LeafName -eq "SPGDemo Old Exports" -and $_.ItemType -eq "Folder" } |
    Select-Object -First 1
$item | Select-Object LeafName, ItemType, DeletedDate

$item | Restore-PnPRecycleBinItem -Force
$back = Get-PnPFolderItem -FolderSiteRelativeUrl "Documents/SPGDemo Old Exports" -ItemType File -Recursive
"SPGDemo Old Exports restored with $($back.Count) files"
Get-PnPRecycleBinItem finding SPGDemo Old Exports and Restore-PnPRecycleBinItem restoring it with 4 files

The recycle bin listed SPGDemo Old Exports as a Folder deleted on 11/1/2026, and the restore brought it back with all 4 files. Filter on ItemType, since a file and a folder can share a name.

OneDrive Folder Delete Cheat Sheet

TaskCommandWhat we saw
Delete a top-level folderRemove-PnPFolder -Name "X" -Folder "Documents" -Recycle -ForceFolder + 4 files recycled
Delete a nested folder-Folder "Documents/Parent/Child"Only the named folder removed
Delete by name patternGet-PnPFolderItem + Where-Object + loop3 folders recycled
Restore a folderGet-PnPRecycleBinItem -FirstStage | Restore-PnPRecycleBinItem -ForceFolder back with 4 files
Delete outrightLeave out -RecycleNot run: can’t be restored from the recycle bin

Common Mistakes to Avoid

The one we hit first was the sign-in. Here’s the same connect line without -Tenant:

try {
    # no -Tenant: PnP guesses the tenant from the -my host name and fails
    Connect-PnPOnline -Url $OneDriveUrl -ClientId $ClientId -DeviceLogin -PersistLogin -ErrorAction Stop
} catch {
    "Connect failed: $($_.Exception.Message)"
}
Connect-PnPOnline failing on a OneDrive URL without -Tenant with an openid-configuration error

PnP looked for a tenant called <tenant>-my.onmicrosoft.com, which doesn’t exist. Adding -Tenant "spguides.onmicrosoft.com" fixed it right away. The second classic is a wrong parent path:

try {
    # wrong parent: the library is "Documents", not "Document" or "My files"
    Remove-PnPFolder -Name "SPGDemo Reports" -Folder "My files" -Recycle -Force -ErrorAction Stop
} catch {
    "Remove failed: $($_.Exception.Message)"
}
Remove-PnPFolder returning File Not Found because the parent path My files is wrong
  • “File Not Found.”: the parent is Documents, not My files or Document, and the folder name must match exactly.
  • “Connect-PnPOnline is not recognized”: the module isn’t loaded in this session. Here’s the fix for Connect-PnPOnline is not recognized.
  • Access denied on someone else’s OneDrive: SharePoint admin alone isn’t enough. Add yourself as a site collection admin of that OneDrive first.
  • Delete blocked or files kept: retention policies and labels win over a delete, so files can stay in the Preservation Hold library.

Does Deleting a Synced OneDrive Folder on My PC Delete It Online?

Yes. The OneDrive sync app treats a local delete like any other change, so deleting the folder in File Explorer, or with Remove-Item, removes it from OneDrive online too. It then sits in the OneDrive recycle bin. If you’re unsure where a file should live, compare OneDrive vs SharePoint for file storage first.

# Deletes the local copy AND, through the sync client, the folder in OneDrive online
$local = Join-Path $env:OneDriveCommercial "SPGDemo Old Exports"
Remove-Item -Path $local -Recurse -Force

We show this one as code only, because our test PC doesn’t sync this OneDrive. If you just want disk space back, use Free up space on the folder instead: the files stay online and only the local copies go.

How Do Admins Delete a Departed User’s OneDrive?

You usually don’t delete it directly. Deleting the user account in Microsoft 365 starts the OneDrive cleanup; blocking sign-in or removing the license doesn’t. The user’s manager (or a secondary owner) gets access and an email, plus a reminder 7 days before the OneDrive is deleted.

The OneDrive is kept for the retention period, 30 days by default (you can set 30 through 3650 days with Set-SPOTenant -OrphanedPersonalSitesRetentionPeriod). After that it stays in a deleted state for 93 days, and only a SharePoint Administrator can restore it.

When you’ve saved what you need, Microsoft’s documented way to delete a OneDrive for good uses the SharePoint Online Management Shell. We show it as code, not run, because we never delete a real OneDrive. If you want to move the files first, see how to move files from OneDrive to SharePoint.

Connect-SPOService -Url "https://spguides-admin.sharepoint.com"

# OneDrives already in the deleted state (after the retention period)
Get-SPODeletedSite -IncludeOnlyPersonalSite | Format-Table Url

# Permanently delete an active OneDrive you no longer need (can't be restored afterward)
$od = "https://spguides-my.sharepoint.com/personal/dmorgan_spguides_onmicrosoft_com"
Remove-SPOSite -Identity $od
Remove-SPODeletedSite -Identity $od

Note: a retention policy or an eDiscovery hold stops the deletion until the hold is removed, and a OneDrive removed with Remove-SPODeletedSite can’t be restored.

Next Steps

Once deletes are under control, the same connection lets you report on, upload to and tidy any OneDrive. Our collection of PnP PowerShell scripts for SharePoint developers has the next scripts we’d reach for, and the SharePoint tutorials hub has the rest.

We’d keep these PnP PowerShell tutorials handy for the next cleanup job:

Frequently Asked Questions

Can I delete a OneDrive folder with PowerShell without the recycle bin?

Yes. Run Remove-PnPFolder without -Recycle and the folder is deleted outright, per the Remove-PnPFolder reference. Keep -Recycle unless you’re sure, because the recycle bin is your only quick undo.

How long can I restore a deleted OneDrive folder?

For work or school accounts, items deleted in the last 93 days can be restored from the OneDrive recycle bin, unless your admin changed that setting. Once an item is removed from the recycle bin, it’s gone.

Can a SharePoint admin delete a folder in another user’s OneDrive?

Only after getting access. Add yourself as a site collection admin of that OneDrive, then connect to its -my URL and run Remove-PnPFolder as usual.

How do I permanently delete a OneDrive site with PowerShell?

Run Remove-SPOSite and then Remove-SPODeletedSite on the OneDrive URL from the SharePoint Online Management Shell. Microsoft’s restore a deleted OneDrive guide warns it can’t be restored after that.