0

I'm working on an arcpy script that removes a feature class and then recreates it. The feature class is part of a replica, and I need to recreate the replica as part of the script, however you cannot unregister a replica using arcpy.

If I manually remove the replica using Distributed Geodatabase > Manage Replicas > Unregister, the script works fine recreating the feature class and the replica, however if I don't manually remove it first the script fails:

Message: ERROR 000775: Replica with the same name already exists. 
Failed to execute (CreateReplica).

I have found my replica in SQL using:

SELECT *
FROM [GDB_ITEMS]
WHERE Type = '4ED4A58E-621F-4043-95ED-850FBA45FCBC'
AND Name = 'MyReplicaName'

Can I "safely" remove this entry in GDB_ITEMS to unregister my replica? If so, I could probably use arcpy to send the SQL to remove it just so I can continue with my script.

By "safely" I mean it's just a single reference and I don't need to remove it from 10 other tables as well to unregister the replica.

If it's relevant, the replica is two-way full SDE to SDE database replica.

Midavalo
  • 29,696
  • 10
  • 48
  • 104

1 Answers1

3

Too bad you aren't using creating replicas from a feature service, then you could just use the REST API to unregister (pretty easy call with Python). I don't have an arcpy solution for you as I don't think it is available there yet.

I do have a Python comtypes ArcObjects solution that works though. I am using a modified version of the Snippets module. The first step is to get a pointer to an IWorkspace object, in my case a SQL Server SDE connection. The following code worked for me:

from arcobjects import *
import comtypes.gen.esriGeoDatabase as esriGeoDatabase  

def unregisterReplica(ws, replicaName=None, replicaID=None, replicaGUID=None):
    """Unregisters a replica from a database

    Required:
        ws -- Pointer to IWorkspace Interface
    Optional (need to use one of these):
        replicaName -- name of replica to unregister
        replicaID -- id of replica
        replicaGUID -- Guid of replica
    """
    wsReplicasAdmin = CType(ws, esriGeoDatabase.IWorkspaceReplicasAdmin2)
    wsReps = CType(ws, esriGeoDatabase.IWorkspaceReplicas)
    replica = None
    if replicaName:
        replica = wsReps.ReplicaByName(replicaName)
    elif replicaID:
        replica = wsReps.ReplicaByID(replicaID)
    elif replicaGUID:
        replica = wsReps.ReplicaByGuid(replicaGUID)

    if isinstance(replica, esriGeoDatabase.IReplica):
        rep_name = replica.Name
        wsReplicasAdmin.UnregisterReplica(replica, True)
        print 'Successfully unregistered: "{}"'.format(rep_name)

if __name__ == '__main__':

    instance = 'sde:sqlserver:ArcSQL'
    ws = Standalone_OpenSDE('SQLServer', instance, database='BMI_PhotoApp')
    name = 'DBO.test_1438722629060'

    reps = unregisterReplica(ws, name)

Please note that you can get at a replica by more than just its name. You can also get a replica by ID/Guid. Have a look at the IWorkspaceReplicas Interface. I have also added this function to my GitHub repo mentioned above.

Also the How do I access ArcObjects from Python post may help you figure out how to set everything up. You need to make sure you compile the .olb files for comtypes first.

crmackey
  • 8,484
  • 18
  • 33
  • Bumping post. It's a few years old but thought I'd see if there was update, as I'm trying to un-register replicas en masse. – TravisB Jul 17 '20 at 22:16
  • @TravisB the solution I posted should work, where you could just do a for loop through a list of SDE Databases passing the database keyword arg. Within each database iteration you could fetch a list of replica ID's and pass the desired ones in. – crmackey Jul 18 '20 at 23:03
  • I found this thread and saw it was dated. I'm trying to do something similar and looking for guidance. You mention if you are using feature services you can use the REST API to unregister. I am looking to automate the unregistering of replicas for feature services so I can automate updating the layers. Is this still possible in ArcPro 2.9 or ArcMap 10.x? I am having difficulty finding documentation on implementing this. Would you be able to expand or assist in finding links? Thank you – Mary Johnson Sep 18 '22 at 19:51