Hello,
I am trying to delete the geoprocessing history of every feature classes in several geodatabases within a particular directory on my machine. I can delete the geoprocessing history of a single feature class, but I currently am unable to walk through every directory and geodatabase.
Please let me know how I can modify the second script to delete the geoprocessing history of several geodatabases.
Thank You
########
CODE TO DELETE GEOPROCESSING HISTORY OF A SINGLE FEATURE CLASS (WORKS)
########
import arcpy
# Create a metadata object for the feature class
md = arcpy.metadata.Metadata(r'C:\mysubfolder\mygeodatabase.gdb\myfeatureclass') # Delete the 'GPHISTORY' metadata item
print(md.description)
md.deleteContent('GPHISTORY')
# Save the updated metadata
md.save()
####ESRI PROVIDED CODE TO WALK THROUGH EACH FOLDER, GEODATABASE, FEATURE CLASS (NOT WORKING YET)
#####################
import arcpy, os
rootDir = r'C:\mysubfolder'
gdbList = []
for dirPath, dirNames, fileNames in os.walk(rootDir, topdown=True):
if dirPath.endswith(".gdb") or ".gdb." in dirPath:
gdbList.append(dirPath)
for gdb in gdbList:
arcpy.env.workspace = gdb
datasetList = arcpy.ListDatasets('*','Feature')
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
FC = arcpy.env.workspace+"\\"+fc
print(FC)
md = arcpy.metadata.Metadata(FC)
print (md)
print(md.description) #test to see if we can pull the description of the current layer
print(md.credits) #test to see if we can pull the description of the current layer
print(md.accessConstraints) #test to see if we can pull the description of the current layer
md.deleteContent('GPHISTORY') #delete GP history
md.save() #save metadata with the history removed
print("Deleted")
for dataset in datasetList:
arcpy.env.workspace = dataset
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
FC = arcpy.env.workspace+"\\"+fc
print(FC)
md = arcpy.metadata.Metadata(FC)
md.deleteContent('GPHISTORY')
md.save()
print("Deleted")
arcpy.env.workspace = gdb