-
Notifications
You must be signed in to change notification settings - Fork 20
Closed
Description
A Python script I wrote to remove Esri default editor tracking fields from a geodatabase.
It could probably use some error handling and cleanup, but it works as-is.
### RemoveEditorTrackingFields.py
### Andrew L. Wunderlich
### andrew.wunderlich@tn.gov
### 7/18/2022
### 5/31/2023 Disables editor tracking if enabled to make field deletion possible
### 2/27/2023 Ported to ArcGIS Pro toolbox
### Removes the editor tracking fields from all classes
### within the user-specified database (if they exist).
### NOTES:
### Editor tracking fields can only be removed if they
### are DISABLED at the database level. This script DISABLES
### Editor Tracking and then deletes the fields.
###
### Searches for the Esri default editor tracking field names.
### If your database uses custom names, edit the field names below
### When adding to a toolbox:
### GENERAL:
### Name: RemoveEditorTrackingFields
### Label: Remove Editor Tracking Fields
### Description: Removes editor tracking fields from the specified geodatabase.
### If editor tracking is enabled, it will be disabled so that fields can be deleted.
### WARNING! This tool modifies the input database. ALWAYS back up your data before using this tool!
### SOURCE:
### Script File: point to this file
### PARAMETERS:
### "Input database" as a Workspace
import arcpy, sys, os.path
import GeMS_utilityFunctions as guf
### Set up workspace
### Use sys.argv[1] when running script from toolbox
#ws = r"c:\temp\testing\input.gdb"
arcpy.env.workspace = sys.argv[1]
ws = arcpy.env.workspace
list_Tables = arcpy.ListTables()
list_FeatureClasses = arcpy.ListFeatureClasses("*", "All", "")
list_Datasets = arcpy.ListDatasets(feature_type='feature')
### Create an empty list that will be populated with field names
fieldNameList = []
### Checks for stand-alone tables
for tb in list_Tables:
#guf.addMsgAndPrint( 'Workspace and table: ' + ws + '\\' + tb)
guf.addMsgAndPrint( 'Checking table ' + tb + ' for editor tracking fields...')
table = ws + '\\' + tb
### Describe the table to check if editor tracking is enabled
desc = arcpy.Describe(table)
if not desc.editorTrackingEnabled:
for f in arcpy.ListFields(tb):
if not f.required:
#guf.addMsgAndPrint( ' Non-required field name: ' + f.name)
if (f.name == 'created_user' or f.name == 'created_date' or f.name == 'last_edited_user' or f.name == 'last_edited_date'):
guf.addMsgAndPrint( ' Field ' + f.name + ' will be deleted from table ' + tb + '...' )
fieldNameList.append(f.name)
#else: #FOR DEBUG
#guf.addMsgAndPrint( ' Skipping ' + tb + ' ' + f.name )
if not fieldNameList == []:
strListOfFields = ', '.join(str(x) for x in fieldNameList)
guf.addMsgAndPrint( ' Deleting ' + strListOfFields + ' from ' + tb + '...' )
arcpy.DeleteField_management(table, fieldNameList)
else:
guf.addMsgAndPrint( ' No editor tracking fields to delete from ' + tb + '...' )
fieldNameList = []
else:
### Editor Tracking IS enabled, disable it before attempting to delete fields
arcpy.DisableEditorTracking_management(tb,"DISABLE_CREATOR","DISABLE_CREATION_DATE","DISABLE_LAST_EDITOR","DISABLE_LAST_EDIT_DATE")
guf.addMsgAndPrint( ' WARNING: Editor tracking has been DISABLED in table ' + tb + '; tracking fields can now be deleted...',1)
for f in arcpy.ListFields(tb):
if not f.required:
#guf.addMsgAndPrint( ' Non-required field name: ' + f.name)
if (f.name == 'created_user' or f.name == 'created_date' or f.name == 'last_edited_user' or f.name == 'last_edited_date'):
guf.addMsgAndPrint( ' Field ' + f.name + ' will be deleted from table ' + tb + '...' )
fieldNameList.append(f.name)
#else: #FOR DEBUG
#guf.addMsgAndPrint( ' Skipping ' + tb + ' ' + f.name )
if not fieldNameList == []:
strListOfFields = ', '.join(str(x) for x in fieldNameList)
guf.addMsgAndPrint( ' Deleting ' + strListOfFields + ' from ' + tb + '...' )
arcpy.DeleteField_management(table, fieldNameList)
else:
guf.addMsgAndPrint( ' No editor tracking fields to delete from ' + tb + '...' )
fieldNameList = []
### Checks for stand-alone feature classes
for fc in list_FeatureClasses:
#guf.addMsgAndPrint( 'Workspace and feature class: ' + ws + '\\' + fc)
guf.addMsgAndPrint( 'Checking stand-alone feature class ' + fc + ' for editor tracking fields...')
featureClass = ws + '\\' + fc
### Describe the feature class to check if editor tracking is enabled
desc = arcpy.Describe(featureClass)
if not desc.editorTrackingEnabled:
for f in arcpy.ListFields(fc):
#guf.addMsgAndPrint( ' Non-required field name: ' + f.name)
if (f.name == 'created_user' or f.name == 'created_date' or f.name == 'last_edited_user' or f.name == 'last_edited_date'):
guf.addMsgAndPrint( ' Field ' + f.name + ' will be deleted from stand-alone fc ' + fc + '...' )
fieldNameList.append(f.name)
#else:
#guf.addMsgAndPrint( ' Skipping ' + fc + ' ' + f.name )
if not fieldNameList == []:
strListOfFields = ', '.join(str(x) for x in fieldNameList)
guf.addMsgAndPrint( ' Deleting ' + strListOfFields + ' from ' + fc + '...' )
arcpy.DeleteField_management(featureClass, fieldNameList)
else:
guf.addMsgAndPrint( ' No editor tracking fields to delete from ' + fc + '...' )
fieldNameList = []
else:
### Editor Tracking IS enabled, disable it before attempting to delete fields
arcpy.DisableEditorTracking_management(fc,"DISABLE_CREATOR","DISABLE_CREATION_DATE","DISABLE_LAST_EDITOR","DISABLE_LAST_EDIT_DATE")
guf.addMsgAndPrint( ' WARNING: Editor tracking has been DISABLED in feature class ' + fc + '; tracking fields can now be deleted...',1)
for f in arcpy.ListFields(fc):
#guf.addMsgAndPrint( ' Non-required field name: ' + f.name)
if (f.name == 'created_user' or f.name == 'created_date' or f.name == 'last_edited_user' or f.name == 'last_edited_date'):
guf.addMsgAndPrint( ' Field ' + f.name + ' will be deleted from stand-alone fc ' + fc + '...' )
fieldNameList.append(f.name)
#else:
#guf.addMsgAndPrint( ' Skipping ' + fc + ' ' + f.name )
if not fieldNameList == []:
strListOfFields = ', '.join(str(x) for x in fieldNameList)
guf.addMsgAndPrint( ' Deleting ' + strListOfFields + ' from ' + fc + '...' )
arcpy.DeleteField_management(featureClass, fieldNameList)
else:
guf.addMsgAndPrint( ' No editor tracking fields to delete from ' + fc + '...' )
fieldNameList = []
### Checks for feature classes within feature datasets
for ds in list_Datasets:
for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
#guf.addMsgAndPrint( 'Workspace and feature class: ' + ws + '\\' + fc)
guf.addMsgAndPrint( 'Checking feature class ' + fc + ' for editor tracking fields...')
featureClass = ws + '\\' + fc
### Describe the feature class to check if editor tracking is enabled
desc = arcpy.Describe(featureClass)
if not desc.editorTrackingEnabled:
for f in arcpy.ListFields(fc):
#guf.addMsgAndPrint( ' Non-required field name: ' + f.name)
if (f.name == 'created_user' or f.name == 'created_date' or f.name == 'last_edited_user' or f.name == 'last_edited_date'):
guf.addMsgAndPrint( ' Field ' + f.name + ' will be deleted from fc ' + fc + '...' )
fieldNameList.append(f.name)
#else:
#guf.addMsgAndPrint( ' Skipping ' + fc + ' ' + f.name )
if not fieldNameList == []:
strListOfFields = ', '.join(str(x) for x in fieldNameList)
guf.addMsgAndPrint( ' Deleting ' + strListOfFields + ' from ' + fc + '...' )
arcpy.DeleteField_management(featureClass, fieldNameList)
else:
guf.addMsgAndPrint( ' No editor tracking fields to delete from ' + fc + '...' )
fieldNameList = []
else:
### Editor Tracking IS enabled, disable it before attempting to delete fields
arcpy.DisableEditorTracking_management(fc,"DISABLE_CREATOR","DISABLE_CREATION_DATE","DISABLE_LAST_EDITOR","DISABLE_LAST_EDIT_DATE")
guf.addMsgAndPrint( ' WARNING: Editor tracking has been DISABLED in feature class ' + fc + '; tracking fields can now be deleted...',1)
for f in arcpy.ListFields(fc):
#guf.addMsgAndPrint( ' Non-required field name: ' + f.name)
if (f.name == 'created_user' or f.name == 'created_date' or f.name == 'last_edited_user' or f.name == 'last_edited_date'):
guf.addMsgAndPrint( ' Field ' + f.name + ' will be deleted from fc ' + fc + '...' )
fieldNameList.append(f.name)
#else:
#guf.addMsgAndPrint( ' Skipping ' + fc + ' ' + f.name )
if not fieldNameList == []:
strListOfFields = ', '.join(str(x) for x in fieldNameList)
guf.addMsgAndPrint( ' Deleting ' + strListOfFields + ' from ' + fc + '...' )
arcpy.DeleteField_management(featureClass, fieldNameList)
else:
guf.addMsgAndPrint( ' No editor tracking fields to delete from ' + fc + '...' )
fieldNameList = []
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels