PowerShell to Delete All Files in SharePoint Document Library

Requirement: Delete All Files in SharePoint Document Library using PowerShell
Delete All Files in SharePoint Document Library using PowerShell

SharePoint PowerShell to Delete All Documents
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to Delete all files in a Folder    
Function Delete-AllFilesFromLibrary([Microsoft.SharePoint.SPFolder]$Folder)
{
    #Delete All Files in the Folder
    Foreach ($File in @($Folder.Files))  
    {
        #Delete the file
        $File.Recycle() | Out-Null

        Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
    }

    #Delete files in Sub-folders
    Foreach ($SubFolder in $Folder.SubFolders | where {$_.Name -ne "Forms"})
    {
        #Call the function recursively
        Delete-AllFilesFromLibrary($SubFolder)
    }
}

#Get the Web and Library
$Web = Get-SPWeb "http://intranet.crescent.com/sales"
$Library = $Web.Lists.TryGetList("Documents")

#Call the function to Delete all files in the Library
Delete-AllFilesFromLibrary $Library.RootFolder
Please note, this script sends all files from all folders-sub folders in a library to recycle bin. If you want to permanently delete files use: $File.Delete() method instead of $File.Recycle().

PowerShell to Delete All Items from SharePoint Library
The above script deletes all files - without deleting folders and sub-folders. What if you want to remove all files and folders?
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to Delete all files in a Folder    
Function Delete-AllFilesFromLibrary([Microsoft.SharePoint.SPFolder]$Folder)
{
    #Delete All Files in the Folder
    Foreach ($File in @($Folder.Files))  
    {
        #Delete the file
        $File.Delete() | Out-Null

        Write-host -f Green "Deleted File '$($File.Name)' from '$($File.ServerRelativeURL)'"
    }

    #Delete files in Sub-folders
    Foreach ($SubFolder in $Folder.SubFolders | where {$_.Name -ne "Forms"})
    {
        #Call the function recursively
        Delete-AllFilesFromLibrary($SubFolder)
    }

    #Delete folders
    ForEach ($SubFolder in @($Folder.SubFolders))
    {
        #Exclude "Forms" and Hidden folders
        If(($SubFolder.Name -ne "Forms") -and (-Not($SubFolder.Name.StartsWith("_"))))
        {
            #Delete the Sub-Folder
            $SubFolder.Delete() | Out-Null
            Write-host -f Green "Deleted Folder '$($SubFolder.Name)' from '$($SubFolder.ServerRelativeUrl)'"
        }
    }
}

#Get the Web and Library
$Web = Get-SPWeb "http://intranet.crescent.com/sales"
$Library = $Web.Lists.TryGetList("Documents")

#Call the function to Delete all files in the Library
Delete-AllFilesFromLibrary $Library.RootFolder 

To delete all files in SharePoint Online, refer: PowerShell to Bulk Delete Files in SharePoint Online
PowerShell to Delete All Files in SharePoint Document Library PowerShell to Delete All Files in SharePoint Document Library Reviewed by Salaudeen Rajack on May 31, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.