How to Remove domain from field for gdb

3966
12
Jump to solution
07-18-2017 11:13 AM
santhoshp
Occasional Contributor
Dear Friends,
Please find the Attached gdb. How to Remove domain from field for gdb and I have multiple domains & names.
See below the domain names.
CD_Level 
CD_FittingDiameter
CD_FittingMaterial 
CD_Diameter
CD_Material 
CD_Ownership 
CD_Parcel ID.
Thanks
Santhosh
0 Kudos
12 Replies
RandyBurton
MVP Alum

Following Pan gis‌'s suggestion, after running the Remove Domain From Field tool from the ArcToolbox in ArcMap, I clicked on the Geoprocessing tab, then copied the Results as a Python snippet.  This will show how ArcMap populates the tool's parameters. 

arcpy.RemoveDomainFromField_management(in_table="PW_Conduit_Wire",
                                       field_name="FLOOR_ID",
                                       subtype_code="'1: Earth NET';'2: ELTW';'3: LTW';'4: Conduit'")‍‍‍

After checking the ListSubtypes documentation, I came up with the following code which should remove the domains from the features using subtypes. 

import arcpy

gdb = r'C:\Path\To\COBW_Power.gdb'
arcpy.env.workspace = gdb

domains = ['CD_Level','CD_FittingDiameter','CD_FittingMaterial',
           'CD_Diameter','CD_Material','CD_Ownership','CD_Parcel ID']

for fds in arcpy.ListDatasets('','Feature'):
    print "DS: {}".format(fds)
    for fc in arcpy.ListFeatureClasses("*","",fds):
        print "\tFC: {}".format(fc)
        subtypes = arcpy.da.ListSubtypes(fc)
        # loops through feature class' subtypes one at a time
        for stcode, stdict in list(subtypes.items()):
            for stkey in list(stdict.keys()):
                # if there is a Subtype Field (that is, it is not an empty string)
                if not stdict['SubtypeField'] == '':
                    st_code = "'{}: {}'".format(stcode, stdict['Name'])
                    if stkey == 'FieldValues':
                        fields = stdict[stkey]
                        for field, fieldvals in list(fields.items()):
                            # if field has a domain
                            if not fieldvals[1] is None:
                                # and the domain is in our list
                                if fieldvals[1].name in domains:
                                    # delete the domain
                                    print("\t\t{} domain deleted from field {} in subtype {}".format(fieldvals[1].name, field, st_code))
                                    arcpy.RemoveDomainFromField_management(in_table=fc, field_name=field, subtype_code=st_code)

print "Done."‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

My previous code should remove the remaining domains from features not using subtypes.

=====

Edit:   After some additional thought, here is a version that will remove domains from fields whether using subtypes or not.  Basically, I added an else on line 21 to pass RemoveDomainFromField a "#" if the feature doesn't use a domain. The indentation, starting at line 23, was changed as a result.

import arcpy

gdb = r'C:\Path\To\COBW_Power.gdb'
arcpy.env.workspace = gdb

domains = ['CD_Level','CD_FittingDiameter','CD_FittingMaterial',
           'CD_Diameter','CD_Material','CD_Ownership','CD_Parcel ID']

for fds in arcpy.ListDatasets('','Feature'):
    print "DS: {}".format(fds)
    for fc in arcpy.ListFeatureClasses("*","",fds):
        print "\tFC: {}".format(fc)
        subtypes = arcpy.da.ListSubtypes(fc)
        # loops through feature class' subtypes one at a time
        for stcode, stdict in list(subtypes.items()):
            for stkey in list(stdict.keys()):
                # if there is a Subtype Field (that is, it is not an empty string)
                if not stdict['SubtypeField'] == '':
                    st_code = "'{}: {}'".format(stcode, stdict['Name'])
                # if no Subtype Field, use "#" in RemoveDomainFromField for subtype_code
                else:
                    st_code = "#"
                if stkey == 'FieldValues':
                    fields = stdict[stkey]
                    for field, fieldvals in list(fields.items()):
                        # if field has a domain
                        if not fieldvals[1] is None:
                            # and the domain is in our list
                            if fieldvals[1].name in domains:
                                # remove the domain
                                print("\t\t{} domain removed from field {} using subtype {}".format(fieldvals[1].name, field, st_code))
                                arcpy.RemoveDomainFromField_management(in_table=fc, field_name=field, subtype_code=st_code)
print "Done."
santhoshp
Occasional Contributor

Dear Randy Burton,

Thank you so much i really appreciate your help.

Thanks

Santhosh

0 Kudos
DavidNovak
Esri Contributor

Thanks @RandyBurton 

super useful.

 

David