Is it possible to automate the delete of points/records?

1083
3
Jump to solution
08-25-2021 01:21 AM
Eva_Useros_
New Contributor

Hello everyone!

I have a survey123 form to make a map about public works. In this forms, the starting date and the ending date are mandatory. Everything works well, but people are expecting that the records would delete automatically once the date of end is passed. So, I've been doing some research but I can't find any clue about how I could make a script (I guess Python script) which can connect to my arcgis online, my feature layer, read the records and delete the records. Maybe, which most worries me is how the make the conection to arcgisonline and the feature layer. 

Anyone has done something similar or has any idea?

Many thanks!

Eva

0 Kudos
1 Solution

Accepted Solutions
GaetanPRU
New Contributor III

Hello,

You can do this with python but you have to plan a task which run script each day, week or month... You can do script like :

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

#connect to GIS
gis = GIS("https://www.arcgis.com", "username", "password", verify_cert=False)

#search feature layer
search_result = gis.content.search(query="id:ITEMID_OF_FEATURE_LAYER", max_items = 1)
featureService = search_result[0]
layer = featureService.layers[X] #change X with ID of the layer where you want to delete entity

#delete feature
#https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html?highlight=delete_features#arcgis.features.FeatureLayer.delete_features
deleteOperation = layer.delete_features(where = "DATEFIELD < DATESELECT")
deleteOperation 
#If delete is OK, result is something like {'deleteResults': [{'objectId': 1, 'success': True}]}

An other way exist with method"edit_features" : https://developers.arcgis.com/python/guide/editing-features/

GaetanPRU

View solution in original post

3 Replies
GaetanPRU
New Contributor III

Hello,

You can do this with python but you have to plan a task which run script each day, week or month... You can do script like :

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

#connect to GIS
gis = GIS("https://www.arcgis.com", "username", "password", verify_cert=False)

#search feature layer
search_result = gis.content.search(query="id:ITEMID_OF_FEATURE_LAYER", max_items = 1)
featureService = search_result[0]
layer = featureService.layers[X] #change X with ID of the layer where you want to delete entity

#delete feature
#https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html?highlight=delete_features#arcgis.features.FeatureLayer.delete_features
deleteOperation = layer.delete_features(where = "DATEFIELD < DATESELECT")
deleteOperation 
#If delete is OK, result is something like {'deleteResults': [{'objectId': 1, 'success': True}]}

An other way exist with method"edit_features" : https://developers.arcgis.com/python/guide/editing-features/

GaetanPRU
jcarlson
MVP Esteemed Contributor

If you throw that script into a Notebook, you can easily schedule it to run from AGOL at regular intervals. A simple script like that would probably cost next to nothing in terms of credits.

- Josh Carlson
Kendall County GIS
DougBrowning
MVP Esteemed Contributor

This is a great way to do it and it is what I do.

One thing to think about is line 5 above.  I grab the ArcPro credentials instead using gis = GIS('pro').  We have smart cards so I can use that to login to Pro and it keeps the connection for 2 weeks.  For us IT security would throw a fit if we hardcoded our username and password in there.  This also may help if you have 2 factor auth.

Also using verify_cert=False is considered bad security and IT would not let us do that either.

I use this code to grab the cert.

import wincertstore

# Loop through the windows trusted certificate store and generate a CA Bundle File in a temporary location
ca_bundle_file = r''
with tempfile.NamedTemporaryFile(delete=False) as tf:
   for storename in ("CA", "ROOT"):
       with wincertstore.CertSystemStore(storename) as store:
           for cert in store.itercerts(usage=wincertstore.SERVER_AUTH):
               # Py v2
               try:
                   tf.write(cert.get_pem().decode("ascii"))
               # Py v3
               except:
                   tf.write(bytes(cert.get_pem(),"ascii"))
   tf.flush()
   ca_bundle_file = tf.name
   tf.close()

# Set the "REQUESTS_CA_BUNDLE" variable
os.environ["REQUESTS_CA_BUNDLE"]=ca_bundle_file

Hope that helps someone.