Need reassurance on Delete Email agent!

As I’ve mentioned before, I was pressed into service as the Notes developer and, although my LScript skills have dramatically improved, there are still scenarios with which I have no experience.I need to delete all emails older than 5 years. I have the code; my question is where do you put the code? In other words, is there a “standard” way of handling agents that act on many databases at once? Currently, I have created an ‘Agent’ database that will be used for scheduled agents like this. I only want this agent to run on Jan. 2 and July 2 every year. I set the agent as scheduled on day 2; in my Initialize, I test for the year and day and it exits if it’s not 1/2 or 7/2. Also, I have selected it to run on “All documents in the database.” Does this setup sound OK? Is there anything I am missing or should do differently? I’m a little paranoid because this agent is deleting emails.

Thanks for your help.

Subject: Need reassurance on Delete Email agent!

why not build view that shows documents older than 5 years. That way you can simply run an agent that loops through the view and deletes them.

At least that way, you will always be able to see whats in the view and be confident of what you’re deleting.

Subject: RE: Need reassurance on Delete Email agent!

Thanks for your reply, Mark; however, as I re-read my original post, I was not very clear. My agent has to delete emails from every employee’s mailfile–about 75 of them. Hence, my code goes into each mail file, finds only the Memos and Replys that are older than 5 years and deletes them. As I told Paul, my concern is more for the agent location and the agent settings. Is it “standard” to put an agent like this in a separate database and run it from there?Thanks.

Subject: RE: Need reassurance on Delete Email agent!

Yes, it is very acceptable to create yourself a central agent database on your mail server for these purposes. I wouldn’t advice setting up the code into, say, the mail template, and replicate it through all replicas: its a lot of work and in the end, it is always dangerous to modify the official mail template in any way.

In a central custom database you can add the lotisscript agent and run it on a schedule. You only need to make sure it runs with the proper ID - someone with admin rights, so that the script actually has the access required to delete the old emails. Make sure the agent is not a restricted agent, otherwise it may not be able to access the file system.

The use of a NotesDbDirectory can help you browse through all the mail files in order to process each email.

I would also use the modified date as a criteria rather than the creation date, just in case someone actually modified an old email recently, and finds it missing the next day…

hth

Nicolas Abesdris

Subject: RE: Need reassurance on Delete Email agent!

Thanks, Nicolas. However, someone suggested that I just create a collection of documents that meet my search criteria. I’m a little new at this and not sure how to build it or even which field I should use. How do I use the Created date or Modified date in my formula? Should I use the ‘Delivered Date’ field? I know my searchString should be something like this:

SearchString$ = {Form = “Memo” | Form = “Reply” & ModifiedDate <= YrAgoDate}

where YrAgoDate is simply today’s date adjusted back the required time; in this case, 5 years.

Any help you can provide is very appreciated; this agent is supposed to run on Jan. 2.

Thanks!

Subject: Posted Code for Delete Email agent!

I am using PostedDate to catch any old mails that were forwarded. Also, I am just testing it in my own mailbox to delete email older than 45 days.Thanks, here’s the code:

Sub Initialize

Dim S As New NotesSession

Dim Db As NotesDatabase

Dim DateToday As String

Dim MonthToday As Integer

Dim DayToday As Integer

DateToday$ = Today()

Set db = S.currentdatabase

’ Check date; agent only runs on January 2 and July 2

MonthToday = Month( DateToday )

DayToday = Day( DateToday )

’ If MonthTo ay = 1 Or MonthToday = 7 Then

’ If DayToday = 2 Then

If MonthToday = 12 Then

If DayToday = 28 Then

Call CleanupEmails

Else

Print “Exited without processing. Agent will process only on January 2 or July 2.”

’ Exit Sub

End If

Else

Print “Exited without processing. Agent will process only on January 2 or July 2.”

Exit Sub

End If

End Sub

Sub CleanupEmails

Dim Dbdir As New NotesDbDirectory( “NotesServer1” )

Dim item As NotesItem

Set YrAgoDate = New NotesDateTime( “Today” )

Call YrAgoDate.AdjustDay( -45 )

’ Call YrAgoDate.AdjustYear( -5 )

Set Db = Dbdir.GetFirstDatabase( DATABASE )

While Not( Db Is Nothing )

’ If Instr(Db.Filepath, “ext”) <> 1 Then

If Db.filepath = “extmail\mherr.nsf” Then

Call Db.Open( “”,“” )

’ Opens currently-selected DB from DbDirectory.

Set docColl = Db.AllDocuments

Set doc = docColl.GetFirstDocument

x = 0

Do While Not( doc Is Nothing )

If doc.HasItem( “PostedDate” ) Then

Flag = 1

Set item = doc.GetFirstItem( “PostedDate” )

IsRFC = item.Type

If IsRFC = 1282 Then

Dim DocDate As New NotesDateTime( doc.PostedDate(0) )

Else

Set DocDate = item.DateTimeValue

End If

Else

Flag = 0

Dim abc As Variant

abc = Evaluate({@Created}, doc)

Dim DocDate1 As New NotesDateTime( abc(0) )

End If

Set docNext = docColl.GetNextDocument( doc )

If Flag = 1 Then

TimeDif = YrAgoDate.TimeDifference( DocDate )

Else

TimeDif = YrAgoDate.TimeDifference( DocDate1 )

End If

If ((doc.Form(0)=“” Or doc.Form(0)=“Memo” Or doc.Form(0)=“Reply” Or doc.Form(0)=“Reply With History” Or _

doc.Form(0)=“(Return Receipt)” Or doc.Form(0)=“Return Receipt” Or doc.Form(0)=“(ReturnNonReceipt)” Or _

doc.Form(0)=“ReturnNonReceipt”)) And TimeDif > 0 Then Call doc.Remove( True )

Set doc = docnext

x = x + 1

Loop

Print "Processed " & x & " documents in " & db.Title & “'s Mailbox.”

End If

Set Db = Dbdir.GetNextDatabase

Wend

End Sub

Subject: Need reassurance on Delete Email agent!

Can you post your code? Also, you don’t want to delete ALL documents, do you? If this is a Delete E-mail agent you really only want to delete docs that use the “Memo” or “Reply” forms and you don’t want to delete mail that is in “draft” mode since this can include stationary and such. Typically you also don’t want to delete calendar and task entries as these tend to be relatively small and won’t add much to the overall size of the mail file unless there are large attachments in them.

Subject: RE: Need reassurance on Delete Email agent!

Thanks for the reply, Paul; however, I think my code is fine. I’ve tested it and I am only deleting Memos and Replys and I’m not touching ToDo or Calendar items. I access each mail file (there’s only about 75) and go through the documents removing the ones that are 5 years or older. I’m more concerned about the agent location and its settings. For instance, should the runtime security level be set at 1 or 2? Also, I’ve read that a scheduled agent will run every time you save it?! If so, how do you prevent that? Right now, the agent is set “On Event.” If it runs when I change it to “Scheduled” on Monday, I’ll be looking for a new job on Tuesday!!Thanks.

Subject: RE: Need reassurance on Delete Email agent!

please post your code. if the agent runs when it’s not supposed to and delete docs then your agent is not setup correctly. you should create a collection of docs that are over 5 years old based on a search, this is the pest way to go about it.

Subject: Posted Code for Delete Email agent!

I am using PostedDate to catch any old mails that were forwarded. Also, I am just testing it in my own mailbox to delete email older than 45 days.Thanks, here’s the code:

Sub Initialize

Dim S As New NotesSession

Dim Db As NotesDatabase

Dim DateToday As String

Dim MonthToday As Integer

Dim DayToday As Integer

DateToday$ = Today()

Set db = S.currentdatabase

’ Check date; agent only runs on January 2 and July 2

MonthToday = Month( DateToday )

DayToday = Day( DateToday )



' If MonthTo ay = 1 Or MonthToday = 7  Then

' If DayToday = 2 Then

If MonthToday = 12 Then

If DayToday = 28 Then



		Call CleanupEmails

	Else

		Print "Exited without processing. Agent will process only on January 2 or July 2."

	'	Exit Sub

	End If

Else	

	Print "Exited without processing. Agent will process only on January 2 or July 2."

	Exit Sub

End If	

End Sub

Sub CleanupEmails

Dim Dbdir As New NotesDbDirectory( "NotesServer1" )

Dim item As NotesItem



Set YrAgoDate = New NotesDateTime( "Today" )

Call YrAgoDate.AdjustDay( -45 )

' Call YrAgoDate.AdjustYear( -5 )



Set Db = Dbdir.GetFirstDatabase( DATABASE )

While Not( Db Is Nothing )

’ If Instr(Db.Filepath, “ext”) <> 1 Then

If Db.filepath = "extmail\mherr.nsf" Then

		Call Db.Open( "","" )

' Opens currently-selected DB from DbDirectory.

		Set docColl = Db.AllDocuments

		Set doc = docColl.GetFirstDocument

		x = 0

		Do While Not( doc Is Nothing )

			If doc.HasItem( "PostedDate" ) Then

				Flag = 1

				Set item = doc.GetFirstItem( "PostedDate" )

				IsRFC = item.Type

				If IsRFC = 1282 Then

					Dim DocDate As New NotesDateTime( doc.PostedDate(0) )

				Else

					Set DocDate = item.DateTimeValue	

				End If

			Else

				Flag = 0

				Dim abc As Variant

				abc = Evaluate({@Created}, doc)

				Dim DocDate1 As New NotesDateTime( abc(0) ) 

			End If

			Set docNext = docColl.GetNextDocument( doc )

			If Flag = 1 Then

				TimeDif = YrAgoDate.TimeDifference( DocDate )

			Else

				TimeDif = YrAgoDate.TimeDifference( DocDate1 )					

			End If

			

			If ((doc.Form(0)="" Or doc.Form(0)="Memo" Or doc.Form(0)="Reply" Or doc.Form(0)="Reply With History" Or _

			doc.Form(0)="(Return Receipt)" Or doc.Form(0)="Return Receipt" Or doc.Form(0)="(ReturnNonReceipt)" Or _

			doc.Form(0)="ReturnNonReceipt")) And TimeDif > 0 Then Call doc.Remove( True )

			Set doc = docnext

			x = x + 1

		Loop

		Print "Processed " & x &  " documents in " & db.Title & "'s Mailbox."

	End If

	Set Db = Dbdir.GetNextDatabase

Wend

End Sub

Subject: RE: Posted Code for Delete Email agent!

your code is all very time consuming and not necessary. just do a Search like below. This will select all documents using the memo and reply forms and that have a PostedDate that is older than 1825 days (5 years).

Dim ns As New notessession

Dim db As NotesDatabase

Dim dt As New NotesDateTime("01/01/2000")

Set db = ns.CurrentDatabase

Dim col As NotesDocumentCollection

srch$ = | Form = ("Memo" : "Reply") & @Date(PostedDate) < @Date(@Adjust(@Today; 0;0;-1825;0;0;0)) |

Set col = db.Search(srch$, dt, 0)

txt$ = "Deleting " & Cstr(col.Count) & " of " & Cstr(db.AllDocuments.Count) & " documents"

Print txt$

Call col.RemoveAll(True)

your code has to cycle through every document in the mail file and skip those that don’t meet your criteria (date & form). The above will select only the docs that meet the search criteria and will delete them. Keep in mind that if soft deletions are enabled it will simply place the docs in the trash.

Subject: Paul, forgot one thing!!

You mentioned that the documents would simply go into the trash if soft deletions was enabled. My instruction is to delete them permanently. I was going to use doc.RemovePermanently in my original code. I was using doc.Remove so I could restore them as I was testing the code. Is there a way to remove them permanently?Thanks.

Subject: RE: Paul, forgot one thing!!

yes, add some code to your agent empty the trash folder.

Subject: RE: Posted Code for Delete Email agent!

Thanks, Paul for your continued help. Actually, I did write a simpler agent and posted it as a response to Nicolas’ post. However, I still have a couple of questions/problems. First, for SOME emails we receive from outside our domain, the datatype for the ‘PostedDate’ field is “RFC822 Text” not date/time!? Hence, that email would not make it into the collection. (That’s why I checked the item.Type in my original code and if it was RFC822… yada, yada)Second, for my own info, what is the difference between the ‘DeliveredDate’ and ‘PostedDate’ when an email is forwarded or copied? Because this agent is also supposed to delete any emails older than 5 years even if they were forwarded.

Thanks, again!

Subject: RE: Posted Code for Delete Email agent!

PostedDate is the sent time; DeliveredDate is the received time.