One of the results of enabling retention policies in SharePoint is that you cannot delete a container, such as a site, list of a folder, without deleting the containing content first.
If you write a script to delete these items you might also hit the throttling threshold, further delaying the detection process
Import-Module SharePointPnPPowerShellOnline
$SiteUrl = "https://dundermifflinpapercompany.sharepoint.com/";
# Can be credentials from file
$UserName = "";
$Password = "";
$List = "DeleteDemo";
$ListCacheFile = "C:\Temp\DeleteDemo.xml";
$CacheTimeoutInMinutes = 30;
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, (ConvertTo-SecureString $Password -AsPlainText -Force)
Connect-PnPOnline -Url $SiteUrl -Credentials $Cred;
$CacheAge = ((Get-Date) - (Get-Item $ListCacheFile).LastWriteTime).TotalMinutes;
if ((test-path -Path $ListCacheFile) -and ($CacheAge -lt $CacheTimeoutInMinutes))
{
# Cache exist and have not expired yet
$items = Import-Clixml -Path $ListCacheFile;
} else {
# Get items from list
$items = Get-PnPListItem -List $List -PageSize 5000|Select-Object ID;
# Save items into cache file
$items|Export-Clixml -Path $ListCacheFile;
}
# Enumerate through all items randomly and remove them
foreach ($item in ($items.GetEnumerator() | Sort-Object {Get-Random}))
{
$item;
Remove-PnPListItem -List $List -Identity $item.Id -force;
}
Import-Module SharePointPnPPowerShellOnline
$SiteUrl = "https://dundermifflinpapercompany.sharepoint.com/";
# Can be credentials from file
$UserName = "";
$Password = "";
$List = "DeleteDemo";
$ListCacheFile = "C:\Temp\DeleteDemo.xml";
$CacheTimeoutInMinutes = 30;
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, (ConvertTo-SecureString $Password -AsPlainText -Force)
Connect-PnPOnline -Url $SiteUrl -Credentials $Cred;
$CacheAge = ((Get-Date) - (Get-Item $ListCacheFile).LastWriteTime).TotalMinutes;
if ((test-path -Path $ListCacheFile) -and ($CacheAge -lt $CacheTimeoutInMinutes))
{
# Cache exist and have not expired yet
$items = Import-Clixml -Path $ListCacheFile;
} else {
# Get items from list
$items = Get-PnPListItem -List $List -PageSize 5000;
# Save items into cache file
$items|Export-Clixml -Path $ListCacheFile;
}
# Enumerate through all items randomly and remove them
foreach ($item in ($items.GetEnumerator() | Sort-Object {Get-Random}))
{
$item;
Remove-PnPListItem -List $List -Identity $item.Id -force;
}
I hope you found this tutorial useful. You are encouraged to ask questions, report any bugs or make any other comments about it below.
Note: If you need more "Support" about this topic, please consider using the Ask a Question feature of Experts Exchange. I monitor questions asked and would be pleased to provide any additional support required in questions asked in this manner, along with other EE experts...
Please do not forget to press the "Thumbs Up" button if you think this article was helpful and valuable for EE members.
It also provides me with positive feedback. Thank you!
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)