0

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

Thanks.

2

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 {
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
}
}

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, privacy policy and cookie policy

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