I need a powershell command which deletes the second stage recyclebin and the possibilities are stated below
Possibilities :
->If it is possible to get a script wherein if we give an user name for ex Deleted By “THEJA,Mr.RAVURI(THEJA)”,it should delete the all the files which were deleted by user.
->Another possibility is get the files based on Deleted which is like, the script should delete all the files which were deleted on specific date.
->to delete all the files which are present in the Admin recycle bin.
->to delete first “n” number of files which are present in recyclebin with ascending or descending order.
Many thanks in Advance and let me know if you require any clarifications on this
Theja.
I Think you are trying to overwrite on previous cmdlet because it is showing $SPSite.RecycleBin.DeleteAll ();
that I am not using. Try to use fresh cmdlet (please close the sharepoint powershell cmd first and then try again.
Another thing, That try to view the name “THEJA,Mr.RAVURI(THEJA)”
Because, I have tested all the above cmdlet before providinig you 🙂
Hi Ajeet,
I get this error when trying this:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SPSiteurl = “https://test.sharepoint.intranet.com/sitebasic“
$site = Get-SPSite -Identity $SPSiteurl
$query = new-object Microsoft.SharePoint.SPRecycleBinQuery;
$query.ItemState = “SecondStageRecycleBin”;
$itemcoll = $site.GetRecycleBinItems($query);
#Delete items those are deleted by Theja
$files= $itemcoll | where {$_.DeletedByName -match “THEJA,Mr.RAVURI(THEJA)”}
#Delete output
foreach ($file in $files) {
$spsite.RecycleBin.Delete($file.ID)
}
Error:
You cannot call a method on a null-valued expression.
At C:\users\theravu-a\Desktop\New Text Document.ps1:11 char:296
+ $SPSite.RecycleBin.DeleteAll ();
+ CategoryInfo : InvalidOperation: (DeleteAll:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Any idea why this comes..:)
Hi Theja,
Here I am with solution for you. 🙂
1- If it is possible to get a script wherein if we give an user name for ex Deleted By “THEJA,Mr.RAVURI(THEJA)”,it should delete the all the files which were deleted by user.
Answer:- yes. here is cmdlet
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SPSiteurl = “https://test.sharepoint.intranet.com/sitebasic“
$site = Get-SPSite -Identity $SPSiteurl
$query = new-object Microsoft.SharePoint.SPRecycleBinQuery;
$query.ItemState = “SecondStageRecycleBin”;
$itemcoll = $site.GetRecycleBinItems($query);
#Delete items those are deleted by Ajeet
$files= $itemcoll | where {$_.DeletedByName -match “Ajeet K. Singh”}
#Delete output
foreach ($file in $files) {
$spsite.RecycleBin.Delete($file.ID)
}
2nd- Another possibility is get the files based on Deleted which is like, the script should delete all the files which were deleted on specific date.
Answer: Yes. here is cmdlet
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SPSiteurl = “https://test.sharepoint.intranet.com/sitebasic“
$site = Get-SPSite -Identity $SPSiteurl
$query = new-object Microsoft.SharePoint.SPRecycleBinQuery;
$query.ItemState = “SecondStageRecycleBin”;
#$query.RowLimit = 2 #Set here ‘n’ numbers of item.I have set it to 2;
$itemcoll = $site.GetRecycleBinItems($query);
#Delete items those are deleted by Ajeet
$files= $itemcoll | where {$_.DeletedDate -match “12/25/2013”}
#Delete output
foreach ($file in $files) {
$spsite.RecycleBin.Delete($file.ID)
}
3rd:-to delete first “n” number of files which are present in recyclebin with ascending or descending order.
Answer: here is your cmdlet
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SPSiteurl = “https://test.sharepoint.intranet.com/sitebasic“
$site = Get-SPSite -Identity $SPSiteurl
$query = new-object Microsoft.SharePoint.SPRecycleBinQuery;
$query.ItemState = “SecondStageRecycleBin”;
#$query.RowLimit = 2 #Set here ‘n’ numbers of item.I have set it to 2;
$itemcoll = $site.GetRecycleBinItems($query);
#Delete output
foreach ($file in $files) {
$spsite.RecycleBin.Delete($file.ID)
}
Thanks for your appreciation.
I don’t understand where you are not able to reply.
Any way, I will try to provide you cmdlet those will fulfill your requirement at earliest.
Hi Ajeet,
I am unable to reply to your new post.The below script which you have sent me worked good.I am very much thankful to you for sharing this.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$SPSiteurl = “https://test.sharepoint.intranet.com/sitebasic“
$spsite = Get-SPSite -Identity $SPSiteurl
$files =$spsite.Recyclebin |?{$_.ItemState -eq ‘SecondStageRecycleBin’}
foreach ($file in $files) {
$spsite.RecycleBin.Delete($file.ID)
}
But my manager has asked me to get a script to delete recylcebin as per below constraints which is my requirement that too if i run the above script for a production site which has heavy files in recyclebin it would result in session failure and may impact to content database.
I would be very happy if you could give the script which deletes with some constaraints as follows:
If it is possible to get a script wherein if we give an user name for ex Deleted By “THEJA,Mr.RAVURI(THEJA)”,it should delete the all the files which were deleted by user.
->Another possibility is get the files based on Deleted which is like, the script should delete all the files which were deleted on specific date.
->to delete first “n” number of files which are present in recyclebin with ascending or descending order.
Many thanks for your valuable time…:)
Theja