SharePoint Online: Delete All Files in a Document Library using PowerShell

Requirement: Delete all documents in library in SharePoint Online

How to Delete All Documents in Library in SharePoint Online?
You can delete multiple files, folders and sub-folders from a SharePoint Online document library in few clicks:
  • Navigate to your SharePoint Online document library in browser, hover over header and click the check mark to select all files.
    sharepoint online delete all files in document library
  • Click on "Delete" button on the top link bar.
  •  In the Delete dialog box, confirm the delete by clicking "Delete" button
    sharepoint online delete all documents in library
  • You'll find the little status box appear in the upper top of the document library telling you that the item has been deleted.
    sharepoint online delete all files
 
SharePoint Online: Delete All Files in Document Library using PowerShell
This PowerShell script deletes all files from a given library - without deleting any folders in it!
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to Delete all files from a given Folder
Function Delete-AllFilesFromFolder([Microsoft.SharePoint.Client.Folder]$Folder)
{
    Try {
        #Get All Files from the Folder
        $Ctx = $Folder.Context
        $Files = $Folder.Files
        $Ctx.Load($Files)
        $Ctx.ExecuteQuery()

        #Iterate through each File in the Root folder
        Foreach($File in $Files)
        {
            #Delete the file
            $Folder.Files.GetByUrl($File.ServerRelativeUrl).Recycle() | Out-Null
            Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
        }
        $Ctx.ExecuteQuery()

        #Process all Sub Folders of the given folder
        $SubFolders = $Folder.Folders
        $Ctx.Load($SubFolders)
        $Ctx.ExecuteQuery()
        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
            }
        }
    }
    Catch {
    write-host -f Red "Error:" $_.Exception.Message
    }
}

#Variables for Processing
$SiteURL = "https://crescenttech.sharepoint.com/sites/Marketing"
$LibraryRelativeURL = "/sites/Marketing/Shared Documents"

#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

#Get the Root Folder of the Library
$RootFolder=$Ctx.Web.GetFolderByServerRelativeUrl($LibraryRelativeURL)
$Ctx.Load($RootFolder)

#Call the function to delete all Files from a Folder
Delete-AllFilesFromFolder -Folder $RootFolder 
Please note, These scripts uses "Recycle" method to send files to recycle bin. You can use "Delete" method to permanently delete files!

PowerShell to Delete All Files and Folders in SharePoint Online Document Library
If you want to delete all files and sub-folders in a library, use:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables for Processing
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"
$LibraryRelativeURL = "/Sites/Marketing/Shared Documents"

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

    #Get the Library and Files
    $Folder=$Ctx.Web.GetFolderByServerRelativeUrl($LibraryRelativeURL)
    $Ctx.Load($Folder)
    $Files = $Folder.Files
    $Ctx.Load($Files)
    $Ctx.ExecuteQuery()

    #Iterate through each File in the Root folder
    Foreach($File in $Files)
    {
        #Delete the file
        $Folder.Files.GetByUrl($File.ServerRelativeUrl).Recycle() | Out-Null
        Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
    }
    $Ctx.ExecuteQuery()

    #Process all Sub Folders
    $SubFolders = $Folder.Folders
    $Ctx.Load($SubFolders)
    $Ctx.ExecuteQuery()
    Foreach($SubFolder in $SubFolders)
    {
        #Exclude "Forms" and Hidden folders
        If( ($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
        {
            #Delete the folder
            $Folder.Folders.GetByUrl($SubFolder.ServerRelativeUrl).Recycle()| Out-Null
            Write-host -f Green "Deleted Folder '$($SubFolder.Name)' from '$($SubFolder.ServerRelativeUrl)'"
        }
        $Ctx.ExecuteQuery()
    }
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
If you want to delete a particular file, use: Delete Files in SharePoint Online using PowerShell
SharePoint Online: Delete All Files in a Document Library using PowerShell SharePoint Online: Delete All Files in a Document Library using PowerShell Reviewed by Salaudeen Rajack on May 28, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.