TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Delete a Data in a List Which are Older than 'n' Number of Days
Vijay S
Apr 22
2016
Code
1.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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