Solved! Go to Solution.
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."
I'm a little unclear on your ultimate objective here. You can easily remove a domain from a field. You can also delete the domain from the geodatabase, which also removes it from all fields it was assigned. I also wrote a script that will delete unused domains from a geodatabase.
EDIT:
Possible duplicate post with https://community.esri.com/thread/198383-how-to-assign-domain-to-field-for-gdb
import arcpy
database = r'...path to database...'
arcpy.env.workspace = database
domains = ['CD_Level','CD_FittingDiameter','CD_FittingMaterial',
'CD_Diameter','CD_Material','CD_Ownership','CD_Parcel ID']
for feature in arcpy.ListFeatureClasses():
for field in arcpy.ListFields(feature):
if field.domain in domains:
arcpy.RemoveDomainFromField_management(feature,field.name)
print "%s domain removed."%str(field.domain)
Dear Holley,
See the below images, it's not removed for domains.
Thanks
Santhosh
It will depend upon, how you had applied the Domain initially.
You cannot remove Domains from fields, which have been assigned under the Subtype tab of the Feature Class properties.
Any domain, assigned under the Fields tab...those can be removed using the tool.
From your images, it looks like you are not indenting the remove domain and print lines correctly. Python us very particular oabout indentation. Take a look at the code posted by mitchh300 again.
Since you are working with feature datasets, the loop should start with ListDatasets. I added a few lines to Mitch Holley's code.
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 "{}".format(fds)
for fc in arcpy.ListFeatureClasses('','',fds):
print "\t{}".format(fc)
for field in arcpy.ListFields(fc):
if field.domain in domains:
arcpy.RemoveDomainFromField_management(fc,field.name)
print "\t\t{} domain removed from field {}.".format(field.domain, field.name)
print "Done."
Dear Randy Burton,
It's working remove domain from field but see the image number 2. I want remove domain from each subtype field.
Thanks
Santhosh
Taking Python out of the equation....did you try simply executing the GP Tool in ArcGIS Desktop?
Hi Santhosh,
by using the tool "Remove domain from field", when a layer with subtypes is input, you get a list of the subtypes, first select all of the subtypes and then choose the field name you want to clear from domains.
The domain will be cleared from all the subtypes.
Then, if your domains are assigned to many fields: either you repeat the GP selecting another field or write a script, export this tool into a scriptand see how it works the script and then loop through a list of field (but I am not the right person for helping in doing so.)