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