Finding Orphaned Domains in a Geodatabase

2600
5
Jump to solution
08-15-2017 04:20 PM
RandyKreuziger
Occasional Contributor III

We have an Enterprise Geodatabase (SDE) containing 300 plus feature classes that acts like a one stop library for our GIS users to use.  There are feature classes from several different state and federal agencies.  Since this GDB goes back over 10 years you can imagine that as feature classes were retired domains were orphaned.  

Has anyone written a program / script to identify orphaned coded value and range domains?

Environment:

  Enterprise Geodatabase 10.4.1

  SQL Server 2012

1 Solution

Accepted Solutions
MicahBabinski
Occasional Contributor III

Hello Randy,

I had to do something similar a while back. How about something like this:

# findOrphanedDomains.py

# import required modules
import arcpy, os

# get the SDE connection as a variable
sdeConnection = arcpy.GetParameterAsText(0) # or hard-code it

# create an empty list that we'll populate with the orphaned domains
orphanedDomains = []

# create an empty list that we'll populate with all the domains in your workspace
allDomains = []

# create an empty list that we'll populate with the applied (non-orphaned) domains in your workspace
appliedDomains = []

# define a function to list the domain names applied to a table or FC
def ListAppliedDomains(table): # could also be a feature class
    """
    Returns a list of domain names applied in the FC or table
    """
    # create empty list of domain names
    appliedDomains = []

    # add any applied domains to the list
    for f in arcpy.ListFields(table):
        if f.domain != "":
            appliedDomains.append(f.domain)

    return appliedDomains


# list the domain objects in your SDE workspace
domainObjects = arcpy.da.ListDomains(sdeConnection)
print("Your SDE workspace has {} domains.".format(str(len(domainObjects))))

# add the names to the list
for domain in domainObjects:
    allDomains.append(domain.name)

# clean up the list of domain objects now that we are done with it
del domainObjects

# list all the feature classes and tables in your SDE workspace
allFcsAndTables = []
walk = arcpy.da.Walk(sdeConnection, datatype=["FeatureClass", "Table"])
for dirpath, dirname, filenames in walk:
    for filename in filenames:
        allFcsAndTables.append(os.path.join(dirpath, filename))

# clean up the walk object
del walk

# go through the tables and feature classes and populate the list of applied domains
for item in allFcsAndTables:
    usedDomains = ListAppliedDomains(item)
    for d in usedDomains:
        appliedDomains.append(d)

# populate the list of orphaned domains based on the 'all domains' that are not in applied domains
for item in allDomains:
    if item not in appliedDomains:
        orphanedDomains.append(item)

# report the result
print("The following domains are not in use in your workspace!")
for item in orphanedDomains:
    print(item)

That should get you there. Feel free to modify to support your specific needs.

Micah

View solution in original post

5 Replies
MicahBabinski
Occasional Contributor III

Hello Randy,

I had to do something similar a while back. How about something like this:

# findOrphanedDomains.py

# import required modules
import arcpy, os

# get the SDE connection as a variable
sdeConnection = arcpy.GetParameterAsText(0) # or hard-code it

# create an empty list that we'll populate with the orphaned domains
orphanedDomains = []

# create an empty list that we'll populate with all the domains in your workspace
allDomains = []

# create an empty list that we'll populate with the applied (non-orphaned) domains in your workspace
appliedDomains = []

# define a function to list the domain names applied to a table or FC
def ListAppliedDomains(table): # could also be a feature class
    """
    Returns a list of domain names applied in the FC or table
    """
    # create empty list of domain names
    appliedDomains = []

    # add any applied domains to the list
    for f in arcpy.ListFields(table):
        if f.domain != "":
            appliedDomains.append(f.domain)

    return appliedDomains


# list the domain objects in your SDE workspace
domainObjects = arcpy.da.ListDomains(sdeConnection)
print("Your SDE workspace has {} domains.".format(str(len(domainObjects))))

# add the names to the list
for domain in domainObjects:
    allDomains.append(domain.name)

# clean up the list of domain objects now that we are done with it
del domainObjects

# list all the feature classes and tables in your SDE workspace
allFcsAndTables = []
walk = arcpy.da.Walk(sdeConnection, datatype=["FeatureClass", "Table"])
for dirpath, dirname, filenames in walk:
    for filename in filenames:
        allFcsAndTables.append(os.path.join(dirpath, filename))

# clean up the walk object
del walk

# go through the tables and feature classes and populate the list of applied domains
for item in allFcsAndTables:
    usedDomains = ListAppliedDomains(item)
    for d in usedDomains:
        appliedDomains.append(d)

# populate the list of orphaned domains based on the 'all domains' that are not in applied domains
for item in allDomains:
    if item not in appliedDomains:
        orphanedDomains.append(item)

# report the result
print("The following domains are not in use in your workspace!")
for item in orphanedDomains:
    print(item)

That should get you there. Feel free to modify to support your specific needs.

Micah

RandyKreuziger
Occasional Contributor III

Wow, that was fast!  

I'll give it a try.

  Thank you!

0 Kudos
MicahBabinski
Occasional Contributor III

Hey Randy, just curious if this worked out for ya! I have to do the same thing myself right now so am going to try it out myself. 

Update: it worked for me!

RandyKreuziger
Occasional Contributor III

Hi Micah,

  Yes, it did.  I'm loving it!

AdamMcClure
New Contributor II

I'm not sure if "orphaned" and "unused" domains are the same or not but I took a look at this post and it helped:

https://community.esri.com/thread/160265 

I didn't want to delete any domains, I just wanted a list of unused ones to narrow the search down (so I didn't execute the DeleteDomain command). When I used Micah Babinski‌'s post above, I got some domains that were actually in use so I wanted to try another method (Note: I'm no python expert and it very well may have been user error).

0 Kudos