Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to Delete a Data in a List Which are Older than 'n' Number of Days
WhatsApp
Vijay S
Apr 22
2016
1.3
k
0
0
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[
void
][System.Reflection.Assembly]::LoadWithPartialName(
"Microsoft.SharePoint"
)
$SiteURL =
"http://mySharepoint"
$site =
new
-
object
Microsoft.SharePoint.SPSite($SiteURL)
$web= $site.OpenWeb()
$documentLibrary = $web.Lists[
"Your List or Library Name"
]
#-10 means files which are older than 10 days will get deleted. so give the count based on your requirement
$yesterdaysDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([System.DateTime]::UtcNow.AddDays(-10.0))
echo $yesterdaysDate
$query =
new
-
object
Microsoft.SharePoint.SPQuery
$query.ViewFields =
"<FieldRef Name='Modified' />"
$query.Query =
"<Where><Leq><FieldRef Name='Modified' /><Value Type='DateTime' IncludeTimeValue='TRUE'>"
+ $yesterdaysDate +
"</Value></Leq></Where>"
$queryItems = $documentLibrary.GetItems($query)
$querycount = $queryItems.Count;
echo $querycount
for
($intIndex = $querycount - 1; $intIndex -gt -1; $intIndex--)
{
$queryItems.Delete($intIndex);
}
$web.Dispose()
$site.Dispose()
delete a data in a list
Up Next
How to Delete a Data in a List Which are Older than 'n' Number of Days