Select to view content in your preferred language

Can you delete offline areas with Python?

1164
7
Jump to solution
01-09-2024 01:26 PM
KansasDASC
Regular Contributor

Building out a FieldMaps application for a field crew, but also trying to lessen the admin burden on them with managing offline map areas. They will be creating offline areas, but since there's a 16-area limit on maps, older offline areas will need to be deleted before new ones are created. Can the ArcGIS API for Python be used to hit those offline areas and delete them? I know it can be used to create/refresh them, but it'd be nice to also delete them programmatically every week or so.

1 Solution

Accepted Solutions
JustinColville
Esri Contributor

Yes, the Python API can be used to remove map areas.  The example code below, removes all map areas for a specific map.

 

from arcgis.gis import GIS
from arcgis.mapping import WebMap

gis = GIS('home')

#The item id of the webmap that you want to delete areas for
offline_map_item_id = '<MAP_ITEM_ID>'
offline_map_item = gis.content.get(offline_map_item_id)
offline_map = WebMap(offline_map_item)

#Remove all existing areas for a map.
for ids in offline_map.offline_areas.list():
    print('Removing map area: ' + ids.title)
    ids.delete()

 

View solution in original post

7 Replies
AaronKoelker
Frequent Contributor

I'm looking for a way to do this as well, but for cleanup purposes whenever staff leave. If the staff didn't remove the offline areas themselves, it's difficult for even admins to find them, and the accounts cannot be deleted while there are items in the account. The map areas don't show up in the owner's Content, both in the GUI or when using the ArcGIS API for Python and checking via user.items(). I've only been able to find and remove them manually using the AGO Assistant.

-Aaron
0 Kudos
JosephRhodes2
Frequent Contributor

Hi Aaron, this will delete a specific user's map areas. I've found the user.items() query to be lacking as well, so I always query user items with gis.content.search, like so:

 

from arcgis.gis import GIS

gis = GIS("home")

username_to_search = ''

user_map_areas = gis.content.search(query=f"owner:{username_to_search}",
                                     item_type='Map Area',
                                     max_items=10000
                                     )

for area in user_map_areas:
    try:
        delete_result = area.delete()
        if delete_result:
            print(f"Map area '{area.title}' ({area.id}) deleted.")  
    except Exception as e:
        print(f"! Deletion failed for map area '{area.title}' ({area.id}). Exception: {e}")

 

 

0 Kudos
JustinColville
Esri Contributor

Yes, the Python API can be used to remove map areas.  The example code below, removes all map areas for a specific map.

 

from arcgis.gis import GIS
from arcgis.mapping import WebMap

gis = GIS('home')

#The item id of the webmap that you want to delete areas for
offline_map_item_id = '<MAP_ITEM_ID>'
offline_map_item = gis.content.get(offline_map_item_id)
offline_map = WebMap(offline_map_item)

#Remove all existing areas for a map.
for ids in offline_map.offline_areas.list():
    print('Removing map area: ' + ids.title)
    ids.delete()

 

AaronKoelker
Frequent Contributor

Thanks for the example @JustinColville , that's good to know. We do have cases however where the web map item gets deleted before the Map Area, and the Map Area continues to persist in the account without the original map. In that case, we might not have the map item id. Is there a way to search for any Map Areas held on an account without needing to know the item id of the map?

-Aaron
0 Kudos
JustinColville
Esri Contributor

@AaronKoelker yes, you should be able to search by type, 'Map Area' and delete the item.  This delete will cascade and delete the related packages (tpks, vtpks and or mobile geodatabases) for the area.

Something like:

from arcgis.gis import GIS
gis = GIS("home")

#checkout the Search doc for more query options
#https://developers.arcgis.com/python/guide/accessing-and-creating-content/#about-search
#https://developers.arcgis.com/python/api-reference/arcgis.gis.toc.html#arcgis.gis.ContentManager.search
#https://support.esri.com/en-us/knowledge-base/how-to-find-specific-items-using-queries-in-the-arcgis-000024383

items = gis.content.search(query='owner:<SOME_USERNAME>', item_type='Map Area')

for item in items:
    print(item)
    item.delete()

 

AaronKoelker
Frequent Contributor

Thanks @JustinColville . That works well. I just tested it on an account that I know holds Map Area items, and the items came up using both your method as well as the one below using user.items(). The latter wasn't working for me before, though, when checking on an account that had all other items manually deleted first. I'm not sure why the Map Areas wouldn't show up in that case, and unfortunately I already deleted those Map Areas manually with the AGO Assistant. Curious if they don't show up if the parent map item doesn't exist anymore and they're "orphaned", so to speak? I'll have to try and test.

Thanks for you help!

user = gis.users.search("a_username_here")
for u in user:
    for i in u.items():
        print(i.title)
-Aaron
0 Kudos
KansasDASC
Regular Contributor

Thanks, @JustinColville. This did the trick. I even managed to take it a step further and define offline map areas older than a certain date so we can selectively delete areas on a weekly basis. Appreciate it!

0 Kudos