Delete All Documents from a SharePoint Document Library using PowerShell

Welcome to an article on how to delete all documents from a SharePoint Document library using PowerShell. Sometimes we need all our files in a document library to be deleted at once for some process or functions which would take place after the library is cleaned.

You have a choice, using this script which will hardly take time to clean your document library.

How? Let’s see it.

  1. Open Windows PowerShell Modules as an Administrator.

    Modules

    Code

    Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null

    #Clean the library using the code below
    #Call the Web
    $weblevel = Get-SPWeb "Add your web application here"
    #Call the library
    $listlevel = $weblevel.Lists["Name of the library"]
    $camls=""
    #We are querying all the items/ documents in the library.
    $querys=new-object Microsoft.SharePoint.SPQuery
    $querys.ViewAttributes = "Scope='Recursive'"
    $querys.Query=$camls

    $itemspres=$listlevel.GetItems($querys)
    #Display the count
    Write-Host $itemspres.Count
    #Delete the documents
    $itemspres | % { $listlevel.GetItemById($_.Id).Delete() }
    #Dispose the web
    $weblevel.Dispose()

  2. Save the above code in a notepad as .ps1 file.

  3. Run the code on the Windows PowerShell Modules.

  4. It will display the number of files deleted.

It will be so quick that you don’t take much time and effort on this, thereby saving a lot of time and effort. Run the script and you have all the files deleted at once.