Help - LS code is somehow creating replication conflicts

My code is set up to add documents to an archive database. It searches for any existing instances of the document it wants to archive, then checks to see if the instance(s) are current or not. Any instance(s) that are not current get deleted. If no current instances are found, a new instance is created. This seems like it should be pretty simple, right? Well, somehow instead of deleting the older instances, it is turning them into replication conflicts. I have tried a few variations in the code, but nothing seems to be working. I am totally confused by this behavior. Please tell me it is something obvious.

The code is running in a scheduled agent signed by me. I have manager level access to the databases.

Also, “Allow soft deletions” is NOT enabled in the archive database.

Here’s the code segment that is producing the repcons:

Set archiveDocCol = archiveView.GetAllDocumentsByKey (keyString, True)

			If archiveDocCol.Count > 0 Then

				For y = 1 To archiveDocCol.Count

					Set archiveDoc = archiveDocCol.GetNthDocument(y)

			'If Not (archiveDoc Is Nothing) Then

					If archiveDoc.originalDateAR(0) < originalDoc.LastModified Then

						Call archiveDoc.Remove (True)

						Msgbox "removing an archive doc found for " & db.ReplicaID & " - " & originalDoc.UniversalID

					Else

						'Msgbox "current archive document found for " & db.ReplicaID & " - " & originalDoc.UniversalID

						createArchiveDoc = False

					End If

				Next 'y

				

			Else

				Msgbox "no previous archive doc found for " & db.ReplicaID & " - " & originalDoc.UniversalID

			End If

			

			If createArchiveDoc Then

				Msgbox "creating an archive doc for " & db.ReplicaID & " - " & originalDoc.UniversalID

				Set newArchiveDoc = originalDoc.CopyToDatabase(archiveDB)

				newArchiveDoc.OriginalIDAR = originalDoc.UniversalID

				newArchiveDoc.OriginalDBIDAR = db.ReplicaID

				newArchiveDoc.OriginalDateAR = originalDoc.LastModified

				newArchiveDoc.OriginalServerAR = db.Server

				newArchiveDoc.StatusAR = "active"

				For x = 1 To 5

					Call newArchiveDoc.RemoveItem("CostCode_" + Cstr(x))

					Call newArchiveDoc.RemoveItem("CostCodeRef_" + Cstr(x))

					Call newArchiveDoc.RemoveItem("CostType_" + Cstr(x))

					Call newArchiveDoc.RemoveItem("CostTypeRef_" + Cstr(x))

					Call newArchiveDoc.RemoveItem("CostDesc_" + Cstr(x))

					Call newArchiveDoc.RemoveItem("CostDescRef_" + Cstr(x))

				Next

				Call newArchiveDoc.Save(True, False)

				archiveCount = archiveCount + 1

			End If

UPDATE: Well, I think I know what’s happening now. The UniversalID of the newly-created document is exactly the same as any documents that would be in the collection. And instead of actually removing the docs in the collection, it is just setting them to an invalid state at this point. So even though they are “removed”, they are still real documents in the database. And when the new document gets created, it generates the repcon. What I really need is a way to clear the collection and flush out the “invalid” docs before I create the new one…

UPDATE 2:

I finally had to give up on using the CopyToDatabase method. I ended up just creating a new blank document in the archive database and then using CopyAllItems. I’m not thrilled with having to use a workaround, but it will have to do. It seems that using CopyToDatabase will always attempt to use the same last 16 characters of the UNID of the source document. So I kept getting identical UNIDs in my archive DB each time I’d make a copy of a given source document. Somehow putting the previous archive document into the invalid state made it possible to create a new archive document with the same UNID. I think that if the previous archive document had not been “removed”, a newly-created archive document would have been forced to generate a new UNID. This is just specualtion for now, though.