Deleting files from a SharePoint Online Document Library using PnP PowerShell is essential for cleanup, automation, and maintenance. With PnP PowerShell, you can efficiently delete single files, multiple files, or files in bulk.
What You’ll Learn:
✔️ How to connect to SharePoint Online
✔️ How to delete a single file
✔️ How to delete multiple files
✔️ How to delete all files in a folder
✔️ How to handle errors and confirmations
Prerequisites
Before proceeding, ensure that:
PnP PowerShell is installed
You have SharePoint Online permissions (Delete rights)
You have the SharePoint site URL
You know the library and folder structure
Step 1: Install and Import PnP PowerShell
If you haven’t installed PnP PowerShell, install it using:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Then, import the module:
Import-Module PnP.PowerShell
PnP PowerShell is ready!
Step 2: Connect to SharePoint Online
Before deleting files, connect to your SharePoint site:
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
🔹 Replace "yourtenant"
with your tenant name
🔹 Replace "yoursite"
with the actual site name
Connected successfully!
Step 3: Delete a Single File from a Document Library
To delete a specific file, provide the file’s server-relative URL:
# Define file path
$fileUrl = "/sites/yoursite/Shared Documents/report.pdf"
# Delete the file
Remove-PnPFile -ServerRelativeUrl $fileUrl -Force
Write-Host "File deleted successfully: report.pdf"
Replace:
"Shared Documents"
with your library name"report.pdf"
with your file name
Single file deleted successfully!
Step 4: Delete Multiple Files from a Document Library
To delete all files in a document library, use this script:
# Define library name
$libraryName = "Shared Documents"
# Get all files in the library
$files = Get-PnPListItem -List $libraryName | Where-Object { $_.FileSystemObjectType -eq "File" }
# Delete each file
foreach ($file in $files) {
$fileUrl = $file.FieldValues.FileRef
Remove-PnPFile -ServerRelativeUrl $fileUrl -Force
Write-Host "Deleted: $fileUrl"
}
This script:
✔️ Gets all files from the library
✔️ Deletes them one by one
All files in the library deleted!
Step 5: Delete All Files from a Specific Folder
To delete files from a specific folder, modify the script:
# Define folder path
$folderPath = "/sites/yoursite/Shared Documents/Reports"
# Get all files in the folder
$files = Get-PnPListItem -List "Shared Documents" | Where-Object { $_.FieldValues.FileRef -like "$folderPath/*" }
# Delete each file
foreach ($file in $files) {
$fileUrl = $file.FieldValues.FileRef
Remove-PnPFile -ServerRelativeUrl $fileUrl -Force
Write-Host "Deleted: $fileUrl"
}
Replace:
"Reports"
with your folder name
All files from the folder deleted!
Step 6: Confirm Before Deleting Files
To add confirmation before deleting, modify the script:
# Define file path
$fileUrl = "/sites/yoursite/Shared Documents/report.pdf"
# Ask for confirmation
$confirm = Read-Host "Are you sure you want to delete this file? (Y/N)"
if ($confirm -eq "Y") {
Remove-PnPFile -ServerRelativeUrl $fileUrl -Force
Write-Host "File deleted successfully."
} else {
Write-Host "Deletion canceled."
}
Added a confirmation step before deleting!
Step 7: Automate File Deletion (Scheduled Task)
If you want to delete old files automatically, schedule this script to run daily:
# Define document library and threshold days
$libraryName = "Shared Documents"
$daysOld = 30
# Get today's date
$cutoffDate = (Get-Date).AddDays(-$daysOld)
# Get files older than 30 days
$files = Get-PnPListItem -List $libraryName | Where-Object { $_.FieldValues.Created -lt $cutoffDate }
# Delete each file
foreach ($file in $files) {
$fileUrl = $file.FieldValues.FileRef
Remove-PnPFile -ServerRelativeUrl $fileUrl -Force
Write-Host "Deleted old file: $fileUrl"
}
Old files automatically deleted!
Step 8: Delete Files from the Recycle Bin (Optional)
When you delete files, they move to the SharePoint Recycle Bin. To permanently delete them, use:
# Empty Recycle Bin
Clear-PnPRecycleBinItem -Force
Write-Host "Recycle Bin emptied successfully!"
Recycle Bin cleared!
Common Errors & Solutions
Error | Cause | Solution |
---|---|---|
Access Denied | Insufficient permissions | Ensure you have Delete rights |
Cannot find file | Incorrect file path | Use Get-PnPListItem to verify the file location |
Object reference not set | File may not exist | Ensure the file exists before running the script |