SharePoint Online: How to Delete a Folder using PowerShell?

Requirement: Delete a folder in SharePoint Online using PowerShell.

SharePoint Online PowerShell to Delete Folder

How to delete a folder in SharePoint Online?

Deleting folders is a great way to clean up your site and eliminate outdated content in SharePoint Online. Deleting a folder is easy and can be completed in just a few steps. This blog post will show you how to delete a folder in SharePoint Online. The first method is to delete the folder directly from the SharePoint site using a web browser like Microsoft Edge or Google Chrome.

  1. To delete a folder in SharePoint Online, select the folder from the list or library and click the “Delete” button from the toolbar. You can also right-click on the folder icon and choose “Delete” from the context menu.
    SharePoint Online PowerShell Delete Folder
  2. Confirm the delete prompt on the delete confirmation dialog box, and you are done!

This will send the folder and contents of the folder to the SharePoint Recycle bin. Deleting a folder using the browser is pretty simple, isn’t it? But sometimes, you may need to delete folders in SharePoint Online or delete multiple folders repeatedly. In such scenarios, using PowerShell is efficient, as it’s quick and easy. How do I delete a folder in SharePoint using PowerShell?

SharePoint Online PowerShell to Delete Folder

There are different ways to delete a folder in SharePoint Online. Here is the PowerShell way to remove a folder from a list or library in SharePoint Online. This PowerShell deletes the folder with its contents, including files and sub-folders.

Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Projects"
$FolderURL="/sites/Projects/Shared Documents/Project 2020" #ServerRelativeURL

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.Web

    #Get the folder object from given URL
    $Folder=$web.GetFolderByServerRelativeUrl($FolderURL)
    
    #sharepoint online powershell delete folder in document library
    $Folder.DeleteObject()
    $Ctx.ExecuteQuery()

    Write-host "Folder deleted Successfully!" -ForegroundColor Green
}
Catch {
    write-host -f Red "Error deleting Folder!" $_.Exception.Message
}

This script deletes the folder permanently. You can use $Folder.Recycle() method to send the folder to the recycle bin. Similarly, you can delete any sub-folder by setting the FolderURL parameter in the above script. You can’t delete a folder from SharePoint Online Management Shell, as there are no cmdlets available.

Delete SharePoint Online Folder using PnP PowerShell

We can also use this PnP PowerShell script to delete a folder in the SharePoint document library: The first step is to connect to your SharePoint Online site. This can be done by running the following cmdlet: Connect-PnPOnline. Once you are connected, you can delete the folder by running the command: Remove-PnPFolder. You will need to replace “Name” with the actual name of the folder you want to delete.

#Config Variables
$SiteURL = "https://crescent.sharepoint.com"
$FolderName = "Classified"
$ParentFolderSiteRelativeURL= "Shared Documents" #ParentFolder

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #sharepoint online powershell delete folder
    Remove-PnPFolder -Name $FolderName -Folder $ParentFolderSiteRelativeURL -Force -Recycle -ErrorAction Stop
    Write-host -f Green "Folder '$FolderName' Deleted Successfully!"
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

To Permanently Delete a Folder, remove the “-Recycle” switch in the Remove-PnPFolder cmdlet. Both the CSOM and PnP PowerShell scripts can be used with OneDrive for Business sites as well to remove a folder. If you want to bulk delete multiple folders from a CSV file, use: Bulk Delete Folders in SharePoint Online from a CSV using PowerShell

Delete a folder in OneDrive using PowerShell

We can also use a similar script to delete a Folder in OneDrive for Business.

#Parameters
$SiteURL = "https://Crescent-my.sharepoint.com/personal/salaudeen_Crescent_com"
$FolderName = "Backup"
$ParentFolderSiteRelativeURL= "Documents" #Default Parent Folder where all OneDrive Files and Folders stored

Try {
    #Connect to OneDrive from PowerShell
    Connect-PnPOnline -Url $SiteURL -Interactive
    
    #PowerShell to Delete a Folder in OneDrive
    Remove-PnPFolder -Name $FolderName -Folder $ParentFolderSiteRelativeURL -Force -Recycle -ErrorAction Stop
    Write-host -f Green "Folder '$FolderName' Deleted Successfully!"
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Unable to delete a folder in SharePoint Online?

Getting the “You have to delete all the items in this folder before you can delete this folder.” error message? Why can’t I delete folders in SharePoint? Well, this could be because of the retention policies applied to your site. So, How to delete a folder with files in SharePoint Online? Well, In this scenario, Here is another script to bulk delete a folder and its contents: SharePoint Online: Delete All Files and Subfolders from a Folder Recursively using PowerShell

How do I restrict users from deleting files and folders in SharePoint?

Create a new permission level from “contribute”, remove all “delete” permissions from it, And then assign the new permission level to the users. This will stop users from deleting any items in SharePoint. However, anyone with more permissions (E.g., “Full Control”) can delete files and folders.
More info: SharePoint Online Prevent Folder Deletion

How do I recover a deleted folder in SharePoint Online?

To restore a folder, navigate to your SharePoint site >> Settings >> “Site Contents” >> Recycle bin. Select the folder to restore from the recycle bin and click on “Restore” If you don’t see the deleted folder in the recycle bin, There is a chance, it may be in the second-stage recycle bin.
More info: Recover Deleted folder in SharePoint Online

How do I delete empty folders in SharePoint Online?

You can find empty folders in a SharePoint Online document library with Item Child Count and Folder Child Count columns and then delete them. PowerShell also can be used to find and delete empty folders in SharePoint Online.
More info: Remove Empty Folders in SharePoint Online

What happens to the files in a deleted folder in SharePoint?

In SharePoint Online, deleting a folder also deletes the files within the folder, which are sent to the site’s Recycle Bin. The items in the Recycle Bin are retained for 93 days after they have been deleted from their original location. The deleted folder and its contents can be recovered from the Recycle Bin during this period. The deleted folder and its contents are permanently deleted if the Recycle Bin is emptied or the 93-day retention period has expired.

How do I delete a document library in SharePoint Online?

To delete a document library in SharePoint Online, there are several options available. One option is to navigate to the library, click on the “Settings” gear icon, select “Library settings,” and select “Delete this document library” under Permissions and Management on the settings page. Another option is to use PowerShell to delete a document library in SharePoint

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

3 thoughts on “SharePoint Online: How to Delete a Folder using PowerShell?

Leave a Reply

Your email address will not be published. Required fields are marked *