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
Hello,
Not sure if you figured out the code or not since you've posted this but I too was having a similar problem. I figured out that you need to reset the workspace so that way it'll look through other datasets for feature classes in a gdb. I made a python toolbox that removes thumbnails and gphistory for every feature class in a gdb.
Hope this helps.
import arcpy
import os
from arcpy import metadata as md
class Toolbox:
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = "toolbox"
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool:
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "GeoprocessingHistoryDeleter"
self.description = ""
def getParameterInfo(self):
"""Define the tool parameters."""
params = []
# Add a parameter for geodatabase file path
param = arcpy.Parameter(
displayName="Geodatabase Path",
name="geodatabase_path",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")
params.append(param)
return params
def isLicensed(self):
"""Set whether the tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
# Get the geodatabase path parameter
geodatabase_path = parameters[0].valueAsText
db_type = "SQL" # Set this to either "SQL", "Oracle", or "Postgres" if your db has spatial views. If not, you may set it to "".
def RemoveHistory(geodatabase_path):
# Removes GP History for feature dataset stored feature classes, and feature classes in the File Geodatabase.
arcpy.env.workspace = geodatabase_path
for fds in arcpy.ListDatasets('', 'feature') + ['']:
for fc in arcpy.ListFeatureClasses('', '', fds):
data_path = os.path.join(geodatabase_path, fds, fc)
if isNotSpatialView(geodatabase_path, fc):
removeMetaData(data_path)
arcpy.AddMessage("Removed the geoprocessing metadata from: {0}".format(fc))
removeMetaData(geodatabase_path)
arcpy.AddMessage("Removed the geoprocessing metadata from: {0}".format(geodatabase_path))
def isNotSpatialView(geodatabase_path, fc):
# Determines if the item is a spatial view and if so returns True to listFcsInGDB()
if db_type != "":
# Check if the feature class is not a spatial view
return True
else:
return True
def removeMetaData(data_path):
# Get the metadata for the dataset
tgt_item_md = md.Metadata(data_path)
# Delete all geoprocessing history from the item's metadata
if not tgt_item_md.isReadOnly:
tgt_item_md.deleteContent('GPHISTORY')
tgt_item_md.deleteContent('THUMBNAIL')
tgt_item_md.save()
if __name__ == "__main__":
RemoveHistory(geodatabase_path)
arcpy.AddMessage("Done Done")
return
def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return