Home > SharePoint, SharePoint 2010 > PowerShell – Delete all files & folders from SharePoint library

PowerShell – Delete all files & folders from SharePoint library

The good thing about PowerShell is that we don’t need to compile anything. Just write the script and execute.

In this article I am going to share a very useful PowerShell script which can be used for clearing SharePoint document library.

Add-PSSnapin Microsoft.SharePoint.PowerShell

# Replace siteurl with actual web url
$web = Get-SPWeb -Identity "siteurl"
# Replace docurl with document library url
$list = $web.GetList("docurl")

function DeleteFiles {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        # Delete file by deleting parent SPListItem
        Write-Host("DELETED FILE: " + $file.name)
        $list.Items.DeleteItemById($file.Item.Id)
    }
}

# Delete root files
DeleteFiles($list.RootFolder.Url)

# Delete files in folders
foreach ($folder in $list.Folders) {
    DeleteFiles($folder.Url)
}

# Delete folders
foreach ($folder in $list.Folders) {
    try {
        Write-Host("DELETED FOLDER: " + $folder.name)
        $list.Folders.DeleteItemById($folder.ID)
    }
    catch {
        # Deletion of parent folder already deleted this folder
    }
}
Advertisements
  1. Patrick
    June 17, 2013 at 9:36 AM

    Nice one. THANKS

  2. September 9, 2013 at 7:55 PM

    Thank you this is exactly what was looking for!

  3. Raghavendra Kumbhar
    September 18, 2013 at 9:30 PM

    i think $web.GetList(“docurl”) doesnt work…you need to change it o
    $web.Lists[“docurl”]

  4. A.m.a.L Hashim
    September 18, 2013 at 9:47 PM

    Hi Raghav, What makes you think it doesn’t work? The above code is tested in multiple environments and it never failed.

  5. May 9, 2014 at 9:32 PM

    Just an FYI… in order to get this script to work on Sharepoint Foundations 2013, I had to make the change that Raghav mentioned.

  6. Dan
    July 2, 2014 at 12:13 AM

    Awesome, THANK YOU

  7. Thinh
    November 25, 2014 at 10:12 PM

    hi, keep getting error on SP 2010 as per below. Please help

    Exception calling “GetList” with “1” argument(s): “0x80070003”
    At E:\Script\PowerShell\SharePointLibraryClearDown.ps1:6 char:21
    + $list = $web.GetList <<<< ("Shared Documents")
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

  8. Darshita
    April 6, 2015 at 4:28 PM

    Exception calling “DeleteItemById” with “1” argument(s): “Value does not fall
    within the expected range.”
    At C:\Developers\Darshita\DeleteFilesNFoldersInDocLib.ps1:16 char:5
    + $list.Items.DeleteItemById($file.Item.Id)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: