Cannot Delete SDE Feature Attachments

503
6
Jump to solution
03-30-2021 09:13 AM
LeviCecil
Occasional Contributor III

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. 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Regular Contributor

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.


Have a great day!
Johannes

View solution in original post

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
0 Kudos
LeviCecil
Occasional Contributor III

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

 

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
0 Kudos
LeviCecil
Occasional Contributor III

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. 

0 Kudos
JohannesLindner
MVP Regular Contributor

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.


Have a great day!
Johannes
0 Kudos
LeviCecil
Occasional Contributor III

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. 

0 Kudos