Remove attachments in batches from feature service?

3032
10
06-23-2021 12:07 PM
SFM_TravisBott
Occasional Contributor III

Is it possible to remove all attachments from a large number of selected features in a feature service all at once, but not remove everything?

As far as I know the only way I've been able to remove attachments is by toggling them off and then back on again. You get a pop-up warning that they'll all be removed. What if I just wanted to remove attachments, from, say 100 features, rather than all of them? Is this possible?

(of note, I'm not sure how to do this on a feature class in Pro, either. Definition queries and selecting features doesn't seem to stop all attachments getting removed). 

0 Kudos
10 Replies
DebMcIlwrath
New Contributor III

Check out the "Remove Attachments" tool in Pro

- select the features you want to remove attachments from and export the selection to a table (uncheck maintain attachments in the environments settings when you do this)

- in the remove attachments tool, use your  feature class is the input data and the exported table is the match table

0 Kudos
SFM_TravisBott
Occasional Contributor III

I'll try that method. 

I'm familiar with the remove attachments tool, but it was removing all attachments on my test feature classes/feature services, regardless of selection, definition query, or creating a separate layer. 

The workaround of exporting a separate table is funky but I'll give it a go. 

0 Kudos
SFM_TravisBott
Occasional Contributor III

Following up, after testing: @DebMcIlwrath 's method works well for a feature class. It does not work for a feature service (which is what I'm trying to accomplish. I received warnings that for the selected records attachments could not be removed, but then they appeared to be removed anyways).

So if you're attempting to selectively remove batches of attachments on a feature service you would have to use a bit of a workaround, like exporting the subset of features, removing their attachments, and then replacing their old version in the feature service. 

0 Kudos
MeganEngel
Occasional Contributor II

Did you try any other methods to accomplish this and were they successful?  I need to perform this workflow on a very large dataset.

 

Thanks.

0 Kudos
JoabelBarbieri
Occasional Contributor II

Any news on this topic?

I need to delete attachments from a FS that are eating my credits away...how do I do that? 

Attachments size: 10558.684 MB

MJBiazar
Esri Contributor

Hi, 

This is possible using ArcGIS API for Python. You can write a script to query features using an expression and delete the attachments of those selected features only while leaving the rest of attachments in the feature service.

Below is a sample code that you can use. Keep in mind that you need to modify some parameters in the script according to the settings in your own data. 

You should be careful when deleting attachments, as this operation is permanent and cannot be undone. It's a good practice to make a backup of your data before performing any deletion operations, just in case you accidentally delete something you didn't mean to.

 

from arcgis.gis import GIS
import arcgis
from arcgis.features import FeatureLayerCollection

gis = GIS("https://www.arcgis.com", "USERNAME", "PASSWORD") #Replace your username and password

featureService = gis.content.get('ITEM_ID')
flayer=featureService.layers[0] #You may need to change layer id from 0 to your own layer number
whereclause = "QUERY_EXPRESSION" #for example "status ='done'" queries features where the value in the "status" field is "done"
for feature in flayer.query(where=whereclause):
    f_oid=feature.attributes.get("OBJECTID") #You need to replace OBJECTID with the name of the objectid field in your dataset.  
    att_list=flayer.attachments.get_list(oid=f_oid)
    if att_list:
        for att in att_list:
            att_id= att['id']
            flayer.attachments.delete(f_oid,att_id)
        print(f"Attachments were deleted for feature {f_oid}")
    else:
        print(f"No attachments found for feature {f_oid}")

 

Some documentation that might be helpful for reference:

https://developers.arcgis.com/python/guide/using-attachments-with-feature-layers/

https://developers.arcgis.com/python/api-reference/arcgis.features.managers.html

 

Best,

MJ

MeganEngel
Occasional Contributor II

This worked like a charm!  Thanks for sharing.  I was able to draft a simple python tool from it with a few dynamic inputs so others can use it within my organization!

 

-M

0 Kudos
heatherdaw
New Contributor III

Will running this code on an ArcGIS Online feature service consume credits? If so, how many (e.g. credits per feature or credits per attachment).

0 Kudos
MeganEngel
Occasional Contributor II

Hey Heath,

As far as i know, this did not consume credits.  I ran in it ArcGIS Pro, not AGO notebooks, which may be why.

0 Kudos