Home » Technology » Deleting All Items from a SharePoint List with PowerShell

Deleting All Items from a SharePoint List with PowerShell

September 18, 2023 by JoyAnswer.org, Category : Technology

How to delete all items from SharePoint list PowerShell? Learn how to use PowerShell to delete all items from a SharePoint list efficiently. Follow a step-by-step guide to streamline your SharePoint data management tasks.


Table of Contents

Deleting All Items from a SharePoint List with PowerShell

How to delete all items from SharePoint list PowerShell?

To delete all items from a SharePoint list using PowerShell, you can use SharePoint Online Management Shell or SharePoint PnP PowerShell cmdlets, depending on your SharePoint environment. Below are steps to achieve this using SharePoint PnP PowerShell:

Prerequisites:

  1. Install SharePoint PnP PowerShell module if you haven't already. You can install it using the following command:

    Install-Module SharePointPnPPowerShellOnline -Force
    
  2. Connect to your SharePoint site using Connect-PnPOnline. Replace https://your-sharepoint-site with the URL of your SharePoint site:

    Connect-PnPOnline -Url https://your-sharepoint-site -UseWebLogin
    

Deleting All Items from a SharePoint List:

Assuming you want to delete all items from a list named "MyList," here's how you can do it:

# Connect to your SharePoint site
Connect-PnPOnline -Url https://your-sharepoint-site -UseWebLogin

# Specify the name or URL of the list you want to delete items from
$ListName = "MyList"

# Get all items in the list
$ListItems = Get-PnPListItem -List $ListName

# Loop through each item and delete it
foreach ($Item in $ListItems) {
    Remove-PnPListItem -List $ListName -Identity $Item.Id -Force
}

# Disconnect from SharePoint site
Disconnect-PnPOnline

Make sure to replace "MyList" with the actual name or URL of the SharePoint list from which you want to delete items. This script will retrieve all items from the specified list and then delete each item one by one.

Remember to run these commands with appropriate permissions, as you need sufficient permissions to delete items from the SharePoint list. Additionally, always be cautious when performing bulk operations like this, as it will permanently delete all items from the list without the possibility of recovery.

Deleting All List Items in SharePoint Online via PowerShell

To delete all list items in SharePoint Online using PowerShell, you can use the following steps:

  1. Connect to SharePoint Online using PowerShell.
  2. Get the list that you want to delete all items from.
  3. Get all items in the list.
  4. Delete each item in the list.
  5. Disconnect from SharePoint Online.

The following PowerShell script deletes all items in a SharePoint list:

PowerShell
# Connect to SharePoint Online
Connect-SPOService -Url https://-tenant-url>.sharepoint.com

# Get the list that you want to delete all items from
$list = Get-SPList -Identity ""

# Get all items in the list
$items = $list.GetItems()

# Delete each item in the list
foreach ($item in $items) {
    $item.Delete()
}

# Disconnect from SharePoint Online
Disconnect-SPOService

Clearing a SharePoint List Using PowerShell Cmdlets

To clear a SharePoint list using PowerShell cmdlets, you can use the following steps:

  1. Connect to SharePoint Online using PowerShell.
  2. Get the list that you want to clear.
  3. Delete all items in the list.
  4. Delete the list.
  5. Disconnect from SharePoint Online.

The following PowerShell script clears a SharePoint list:

PowerShell
# Connect to SharePoint Online
Connect-SPOService -Url https://-tenant-url>.sharepoint.com

# Get the list that you want to clear
$list = Get-SPList -Identity ""

# Delete all items in the list
$items = $list.GetItems()
foreach ($item in $items) {
    $item.Delete()
}

# Delete the list
$list.Delete()

# Disconnect from SharePoint Online
Disconnect-SPOService

Scripting Solutions to Bulk Delete SharePoint Items

In addition to the above methods, there are also a number of scripting solutions that can be used to bulk delete SharePoint items. One such solution is the SharePoint Online Management Shell, which provides a number of cmdlets for managing SharePoint lists and items.

Another solution is the PnP PowerShell library, which is a third-party library that provides a number of cmdlets for managing SharePoint and Microsoft 365.

The following PowerShell script uses the PnP PowerShell library to bulk delete SharePoint items:

PowerShell
# Import the PnP PowerShell library
Import-Module PnP.PowerShell

# Get the list that you want to bulk delete items from
$list = Connect-PnPOnline -Url https://-tenant-url>.sharepoint.com -List ""

# Get all items in the list
$items = $list.GetItems()

# Bulk delete the items in the list
$list.DeleteItems($items)

Whichever method you choose, it is important to carefully test your script before using it in a production environment.

Tags SharePoint , PowerShell Scripting

People also ask

  • What is a PowerShell script?

    A PowerShell script is also provided to facilitate management tasks related to setting up and maintaining cross-forest certificate enrollment environment. Copyright Information This document is provided for informational purposes only and Microsoft makes no warranties, either express or implied, in this document.
    Delve into the world of PowerShell scripts, understanding their role in automating tasks and managing system operations. ...Continue reading

The article link is https://joyanswer.org/deleting-all-items-from-a-sharepoint-list-with-powershell, and reproduction or copying is strictly prohibited.