Delete all Files in SharePoint - Fabian Neve

About SharePoint, Javascript, PowerShell, and other technical stuff

Monday, October 5, 2015

Delete all Files in SharePoint

How to delete every single document in every single site in a site collection? Here is how:

$site = Get-SPSite "http://yourwebapplication/sitecollection"
foreach ($web in $site.AllWebs) {
     
    $Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary"}
 
    foreach ($library in $Libraries) {
 
        Write-Output "Getting files from $($library.Title)"
        $Files = $library.Items | where {$_.FileSystemObjectType -eq "File"}
        foreach ($file in $Files) {
            Write-Output "Deleting file $($file.Name)..."
            $file.Delete()
         
        } # foreach file
     
    } # foreach library
     
} # foreach web


What if you want to delete only specific MS Office like docx or pptx?

$site = Get-SPSite "http://collaboration-dev/team"
foreach ($web in $site.AllWebs) {
     
    $Libraries = $web.Lists | where {$_.BaseType -eq "DocumentLibrary"}
 
    foreach ($library in $Libraries) {
 
        Write-Output "Getting files from $($library.Title)"
        $Files = $library.Items | where {$_.FileSystemObjectType -eq "File" -And ($_.File -Like "*.docx" -Or $_.File -Like "*.ppt" -Or $_.File -Like "*.pptx")}
        foreach ($file in $Files) {
            Write-Output "Deleting file $($file.Name)..."
            $file.Delete()
         
        } # foreach file
     
    } # foreach library
     
} # foreach web



Source:

No comments:

Post a Comment