I need a script to check the attribute indexes of every feature class in a geodatabase, to delete or create the indexes to fit my needs.
I've used the ListIndexes arcpy function but it lists not only attribute indexes but also spatial indexes.
Is there a way to list only attribute indexes?
import arcpy
fields = "OBJECTID"
index_name = "FDO_OBJECTID"
unique_vals = "UNIQUE"
order = "ASCENDING"
FSO_OID = False
try:
gdb = arcpy.GetParameterAsText(0)
arcpy.env.workspace = gdb
fdatasets = arcpy.ListDatasets()
for fdataset in fdatasets:
arcpy.AddMessage("Feature dataset: " + fdataset)
arcpy.AddMessage('-' * 40)
fcs = arcpy.ListFeatureClasses('','',fdataset)
for fc in fcs:
indexes = arcpy.ListIndexes(fc) # ----> Lists both attribute and spatial indexes!!!
for index in indexes:
if (index.name <> index_name):
arcpy.RemoveIndex_management(fc, index.name)
else:
FSO_OID = True
if FSO_OID == False:
arcpy.AddIndex_management(fc, fields, index_name, unique_vals, order)
except:
arcpy.GetMessages(2)
What I don't know is if *all* spatial indexes have the word "Shape" in them. If so, then you can just test for this string value in the index name:
'skip over any index name with string Shape in it
if not "Shape" in index.name:
arcpy.RemoveIndex_management(fc, index.name)
Or alternatively, can you just skip over the ObjectID field to check for attribute indexes? (I am not sure if that is sufficient because I don't know if all spatial indexes are found on that field).
'describe each featureclass and build a list of indexes on each
for fc in fcs:
dsc = arcpy.Describe(fc)
listOfIndexes = [idx.name for idx in dsc.Indexes][1:]
I question the validity of simply skipping over the OID field as this. When I look at the properties of a FeatureClass and see it's spatial index name "FDO_Shape", it also shows up in the listOfIndexes list when run. So, it appears to not catch it by simply skipping over the OID.Sorry for a non-direct answer, but maybe you can sort it out from here.