I have a script that exports and renames photo attachments from an sde feature class. I need to delete the existing attachments each time the script runs, but I can't get it to work. I've tried arcpy.RemoveAttachments_management and del Attachment, but they persist.
Solved! Go to Solution.
If you only want to delete the attachments, but keep the features they are attached to:
attach_table = 'CustodianInspection__ATTACH'
##Step 1 -Search the atttachment table and export as jpegs with REL_GUID in filename
## Use UpdateCursor instead of SearchCursor
with da.UpdateCursor(attach_table, ['DATA', 'ATT_NAME', 'REL_GLOBALID']) as cursor:
for item in cursor:
attachment = item[0]
file_GUID = "GUID_" + str(item[2])
filename = file_GUID + str(item[1])
open(temp_dir + os.sep + filename, 'wb').write(attachment.tobytes())
cursor.deleteRow() # Delete this attachment
del item
del attachment
If you don't need the features whose attachments you have exported, you can just delete the features and the attachments are deleted automatically.
Are you getting errors? or is it a permissions thing?
Did you try the alternate method described in the Usage section?
Remove Attachments (Data Management)—ArcGIS Pro | Documentation
Hi Dan,
I did not get any errors. Here is what I tried when the remove attachment tool didn't work. This also did not work:
attach_table = 'CustodianInspection__ATTACH'
##Step 1 -Search the atttachment table and export as jpegs with REL_GUID in filename
with da.SearchCursor(attach_table, ['DATA', 'ATT_NAME', 'REL_GLOBALID']) as cursor:
for item in cursor:
attachment = item[0]
file_GUID = "GUID_" + str(item[2])
filename = file_GUID + str(item[1])
open(temp_dir + os.sep + filename, 'wb').write(attachment.tobytes())
del item
del attachment
It looks like you are writing out the the attachment (?) to a file during the process.
From what I can't tell is whether you want to actually delete a file that is used as an attachment. All you are doing in that script is deleting the reference to the name. I would suspect that you have to delete using the "os" module
Hey Dan,
Yes, we're exporting the attachments daily to files in directories by site and date taken. What I want to do is clear out the attachment table each time the script runs. Otherwise I'm rewriting these every time the script runs and there are new photo attachments.
If you only want to delete the attachments, but keep the features they are attached to:
attach_table = 'CustodianInspection__ATTACH'
##Step 1 -Search the atttachment table and export as jpegs with REL_GUID in filename
## Use UpdateCursor instead of SearchCursor
with da.UpdateCursor(attach_table, ['DATA', 'ATT_NAME', 'REL_GLOBALID']) as cursor:
for item in cursor:
attachment = item[0]
file_GUID = "GUID_" + str(item[2])
filename = file_GUID + str(item[1])
open(temp_dir + os.sep + filename, 'wb').write(attachment.tobytes())
cursor.deleteRow() # Delete this attachment
del item
del attachment
If you don't need the features whose attachments you have exported, you can just delete the features and the attachments are deleted automatically.
That did the trick, thank you! We want to keep the features, as these attachments are pictures of rooms taken by custodians to keep track of issues.