1

I'm using the following script to try and delete items from a Doc Library rather unsuccessfully. There are no files in the root, just all in subfolders under this doc library. Where am I going wrong on getting it to search the folders and remove from there?

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$urlSite = “”    
$listname = “Documents”    
write-host "Opening site..."    
$web=get-spweb $urlSite    
write-host "Opening list..."    
$list=$web.lists | ? { $_.title -eq $listName}    
$spQuery= New-Object Microsoft.SharePoint.SPQuery    
$spQuery.ViewAttributes="Scope='RecursiveAll'";    
$spQuery.RowLimit = 200    
$caml = "<OrderBy><FieldRef Name='ID' Ascending='False'/></OrderBy>"    
$spQuery.Query=$caml    
do    
{    
    $listItems = $list.GetItems($spQuery)    
    $count=$listItems.Count    
    $spQuery.ListItemCollectionPosition= $listItems.ListItemCollecitonPosition    
    $batch="<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>"    
    $j=0    
    for($j=0;$j -lt $count; $j++)    
    {    
        $item=$listItems[$j]    
        $command="<Method>"    
        $command+="<SetList Scope='Request'>$($list.ID)</SetList>"    
        $command+="<SetVar Name='ID'>$($item.ID)</SetVar>"    
        $command+="<SetVar Name='owsfileref'>$($web.Url)/$($item.Url)</SetVar>"    
        $command+="<SetVar Name='Cmd'>Delete</SetVar>"    
        $command+="</Method>"    
        $batch+=$command    
        if($i -ge $count){break}
    }
    $batch+="</Batch>"
    write-host "Sending batch..."   
    $result=$web.ProcessBatchData($batch)   
    write-host "Emptying Recycle Bin..."    
    $web.RecycleBin.DeleteAll()
}
while($spQuery.ListItemCollectionPosition -ne $null)
$web.Dispose()
write-host "Process finished"

I need to get 100,000 items odd items deleted and it seems like this is the only method but I can't quite get it working.

1

http://www.dmcinfo.com/Latest-Thinking/Blog/articleType/ArticleView/articleId/8471/Using-SPWebProcessBatchData-to-Update-Document-Library-Properties.aspx

Summary: When running ProcessBatchUpdate on a document library you need to pass the server relative url of the file in each item. Use SPListItem.File.ServerRelativeUrl or SPListItem.Folder.ServerRelativeUrl.

Change the following line...

$command+="<SetVar Name='owsfileref'>$($web.Url)/$($item.Url)</SetVar>"

to

$command+="<SetVar Name='owsfileref'>$item.File.ServerRelativeUrl</SetVar>"
| improve this answer | |
  • This doesn't seem to recurse the folder structure in the document library still. – PipBoy Jan 13 '15 at 9:49
  • 1
    Try use...$spQuery.ViewAttributes="Scope='Recursive'" – Avijit Sur Jan 13 '15 at 10:08
  • Same effect, doesn't recurse the folder structures. – PipBoy Jan 13 '15 at 10:10

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.