SHAREPOINT DELETE BULK LIST DATA WITH POWERSHELL
SharePoint Online/On Premises provides you with the choice of using SharePoint Management Shell or Powershell.
So let us quickly ‘get our hands dirty’
Using SharePoint Management Shell (We are using SharePoint 2013)
- Open SharePoint Management Shell in SharePoint 2013 server
- On the Shell dialog box, type the below code
$web = get-spweb link-to-site-or-subsite
$list = $web.lists | where {$_.title -eq “name-of-list-or-library”}
Write-host “List $($list.title) has $($list.items.count) entries”
$items = $list.items
foreach ($item in $items)
{
Write-host “ Say Goodbye to $($item.id)” -foregroundcolor red
$list.getitembyid($Item.id).Delete()}
Hold on a min…
Confusing right?? Now let’s explain what the code does
$web = get-spweb link-to-site-or-subsite
$web is a variable name, you could as well type just get-spweb link-to-site-or-subsite but we might need to use this at different points in our script, it is a clean approach to use a variable.
get-spweb link-to-site-or-subsite
get-spweb https:contoso-site/subsite this line would simply get the website where the list or library resides.
$list = $web.lists | where {$_.title -eq “name-of-list-or-library”}
Here $list is another variable referencing the name of the list or library where the data would be deleted from so, name-of-list-or-library could be test-list or testLibrary (any SharePoint library).
Write-host “List $($list.title) has $($list.items.count) entries”
This line would display a count of how many data the list/library contains e.g test-list has 24 entries
$items = $list.items
foreach ($item in $items)
{
Write-host “ Say Goodbye to $($item.id)” -foregroundcolor red
$list.getitembyid($Item.id).Delete()}
You might need a bit of programming knowledge to understand this loop, but don’t bother if it seems strange, just copy and paste.
This foreach snippet basically just iterates into each data.
Write-host “ Say Goodbye to $($item.id)” -foregroundcolor red
What does this line do? Now after deleting the first data, this line would display in red colour: Say Goodbye to 1, next would be Say Goodbye to 2, You get right?
$list.getitembyid($Item.id).Delete()
This line would delete any data foreach iterates over.
Awesome right?
If you run this script in PowerShell, You might see an error: ‘get-spweb is not a recognised command’ so ensure you run
Add-PSSnapin Microsoft.Sharepoint.Powershell
This line would import the get-spweb module, so type this line, hit enter key before running those codes, to avoid the mentioned error.
……I hope this made sense….Do drop your comments below, till my next write Up.. Byeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!