2

I have multiple lists with 100k+ items. I've created views with old/retired data that I need to delete. I've named the views "tobedeleted". Is it possible to run a powershell script to delete only the list items found in the specific "tobedeleted" view?

This is for SharePoint 2013 on premise.

  • If you plan to run this script on a regular basis you should consider Not to use the view to define those items, as somebody else might change the view. I learned that the hard way – Kasper Bo Larsen Nov 18 '18 at 8:58
2

This should work:

$site = get-spsite http://yourServer/sites/yourSite
$web = $site.RootWeb  # change to select your site
$list = $web.Lists["Test"]
$view = $list.Views["View1"]
$items = $list.GetItems($view)
for ($i=$items.count-1; $i -ge 0; $i--) { $item = $items[$i]; $item.Delete(); }

Notes:

  • This will only delete the number of items normally displayed in the view. I.e. 30 by default.
  • You may need to run this in batches of 5000 or less.
  • Note that the loop counts down, not up.
  • To move the items to the recycle bin, call .Recycle() instead of .Delete().
  • To add a "count down" so you can see how far along it is, change the "for" loop to add a "write-host":
for ($i=$items.count-1; $i -ge 0; $i--)
{ 
   $item = $items[$i]; 
   $item.Delete(); 
   Write-host -NoNewLine "$i        `r"
}

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.