Is there a way to delete a Geoportal service using ArcGIS Python API without using the item id? I need to delete a geocode service and publish it with the exact same name, weekly.
If I use the "itemid" than I can't make this an automated task, because each time you publish a service the "itemid" changes.. I know I can search for an item, but you can't delete an item from "gis.content.search".
I am using gis.content.get()
Could you search for the item title and type, extract the ID, then pass that into the delete method?
from arcgis import GIS
gis = GIS("https://portal.domain.com","administrator","administratorpassword",verify_cert=False)
itemID = gis.content.search("SampleWorldCities","Map Service")[0].id
item = gis.content.get(itemID)
item.delete()
A service's title is not unique, so there's no way for the API to know which item you're intending to delete. As long as you're careful in maintaining a single item with that name, you could do something like the following:
# Search for the item by name, return the itemid
item_id = gis.content.search('the specific name of the item', item_type='the item type')[0].id
# Reference the returned ID in the delete function
gis.content.delete_items([item_id])
Alternately, if you trust your search to always return the single item you want, you can also do:
gis.content.search('the specific name of the item', item_type='the item type')[0].delete()
And as a final alternative, you can avoid the itemid altogether by going straight to the Service that you have published.
server = gis.admin.servers.get('HOSTING_SERVER')[0]
service = server.content.find('service name').service.delete()
item_id = gis.content.search(query='title:PARCEL_GEOCODE_TEST_2', item_type='Geocoding Layer')
Finding the item works, but I am having issues getting the id to print. If I put [0].id I get an error; IndexError: list index out of range..
That would mean your search is returning no items? But you say that finding it by itself works, so that would be strange. It may be simpler to take the server approach instead.
In your image, ItemID is a list of items, as denoted by the brackets. You need to call the list item by using ItemID[0], then you can get the ID from it.