ERROR

Runtime error: item <item_name> already exists

Last Published: May 28, 2020

Error Message

It is possible to overwrite an existing hosted feature layer in a portal using the overwrite() method in ArcGIS API for Python, as shown in the following sample:

from arcgis.gis import GIS
from IPython.display import display

gis = GIS("https://python.playground.esri.com/portal", "username", "password", verify_cert=False)

item = gis.content.search("feature_name", "Feature Layer")

service_flayer_collection = FeatureLayerCollection.fromitem(item)

service_flayer_collection.manager.overwrite(r'[folder_location]/TEST.zip')

However, in some cases, the following error is returned:

Error:
Runtime error: item <item name> already exists.

Cause

The error may occur if the published layer is associated with a view layer. Refer to Portal for ArcGIS: Create hosted feature layer views for more information. When searching for the feature layer name with the gis.content.search() function, more than one result is returned: the feature layer and the view layer. To check for any associated view layers, the parameter set for the gis.content.search() function can be called in the script, as shown in the following sample:

item = gis.content.search("featurename", "Feature Layer")
item

The code returns all available items with the specified name. The following script shows a sample of the result:

[<Item title:"featurename_1" type:Feature Layer Collection owner:User_1>, <Item title:"featurename" type:Feature Layer Collection owner:User_1>]

Solution or Workaround

To counter the issue of the gis.content.search() function, and index or array can be added to the function. As view layers are listed first in the search result, an index of '1' must be inserted for the search to select the right hosted feature layer to be overwritten. The following code sample shows how to do so:

from arcgis.gis import GIS
from IPython.display import display

gis = GIS("https://python.playground.esri.com/portal", "username", "password", verify_cert=False)

#For using index array method
item = gis.content.search("feature_name", "Feature Layer")[1] //Setting the index for the search function

service_flayer_collection = FeatureLayerCollection.fromitem(item)

service_flayer_collection.manager.overwrite(r'[folder_location]/TEST.zip')

Another alternative is to use the gis.content.search() method by searching for the item ID of the desired feature. The item ID of a feature can be found on the address bar of a browser when the feature is opened in ArcGIS Online as shown in the following image:

The image of an address bar of an opened Hosted Feature Layer

To do so, modify the script as follows:

#For searching using item ID
item = gis.content.search("item_id")

Article ID: 000019281

Software:
  • ArcGIS API for Python 1 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options