none
Deleting files from SharePoint online document library from Powershell RRS feed

  • Question

  • Hi Guys.

    I'm going around in circles currently trying to find, what i thought would be a fairly simple, script. I need to delete the contents of a document library from a SharePoint Online site using PowerShell.

    So far I keep finding scripts that are specifically NOT for SharePoint online or, ones that want to delete everything in the site including the document library.

    If anyone is able to help me on this, i would be externally grateful!!

    Many Thanks in advance!

    Martin

    • Moved by Bill_Stewart Wednesday, September 5, 2018 10:05 PM This is not "research stuff for me" forum
    Monday, July 2, 2018 2:06 PM

All replies

  • You need to download and install the SharePoint online shell and support modules.

    Also, since your issue is product specific,  post your questions in the SharePoint forum. 


    \_(ツ)_/

    Monday, July 2, 2018 2:21 PM
  • Hi Guys.

    I'm going around in circles currently trying to find, what i thought would be a fairly simple, script. I need to delete the contents of a document library from a SharePoint Online site using PowerShell.

    So far I keep finding scripts that are specifically NOT for SharePoint online or, ones that want to delete everything in the site including the document library.

    If anyone is able to help me on this, i would be externally grateful!!

    Many Thanks in advance!

    Martin

    Hello Martin,

    You can use PnP Powershell (https://github.com/SharePoint/PnP-PowerShell). Then if the framework is installed you can have a script of this style.

    Clear-Host
    
    # Site Credential
    $mycredential = Get-Credential
    
    # Connect to the site with PnP Module
    Connect-PnPOnline -Url "<your site>" -Credentials $mycredential
    
    # enumerate all lists begin with Library Test
    $lists = Get-PnPList | Where-Object {$_.Title -like "Library Test*"}
    foreach ($list in $lists)
    {
        $listname = $list.Title
        # Get Items for a specific list
        $items = Get-PnPListItem -List $listname
        # Remove all items present in this list
        foreach ($item in $items)
        {
            $url = $item.FieldValues.FileRef
            Remove-PnPFile -ServerRelativeUrl $url -Force
        }
    }

    Best regards.

    • Proposed as answer by gjayne Thursday, September 13, 2018 11:22 AM
    Wednesday, July 4, 2018 1:41 PM