I want to delete all files and folders in shared document using powershell.


1 Answer 1


try below code:

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 {
$folder = $web.GetFolder($folderUrl)
foreach ($file in $folder.Files) {
    # Delete file by deleting parent SPListItem
    Write-Host("DELETED FILE: " + $file.name)

# Delete root files

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

# Delete folders
foreach ($folder in $list.Folders) {
try {
    Write-Host("DELETED FOLDER: " + $folder.name)
catch {
    # Deletion of parent folder already deleted this folder

reference: https://amalhashim.wordpress.com/2013/02/26/powershell-delete-all-files-folders-from-sharepoint-library/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.