Adding Delete Protection for ArcGIS Online Content Items with the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, updating, and administering components of ArcGIS Online such as content item capabilities and settings. In this post we will assess protecting your content items to prevent accidental deletion. This is especially important when you are programmatically performing deletions because there will be no “are your sure?” prompt.  

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. 

				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS

				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

Item object - protected property

An Item object has a property named protected which has a Boolean value, True if delete protection has been set, otherwise False.
				
					## get content item as an item object
item = agol.content.get("ITEM_ID")

## print the item object protected property value
print(item.protected)

>>> False
				
			

If I check the Settings tab for this item in ArcGIS Online I can see that Delete Protection has not been enabled, hence False returned above.

Set delete protection to true

An Item object has a protect() method that can be used to control the delete protection setting. Below we are enabling delete protection on the item.
				
					## set delete protection to true
item.protect()
print(item.protected)

>>> True
				
			

Let’s refresh the Settings page and see that Delete Protection has been set and the Delete Item button is not available for use.

Attempt to delete the item

Let’s try and delete the item!

				
					## attempt to delete
item.delete()

>>> Exception: Unable to delete item 8a9e806f0e8c47e08fc19c17a726a39c. Delete protection is turned on.
				
			

We get an error stating that Delete protection is turned on.

Set delete protection to false

We can of course remove the Delete Protection barrier if we want the ability to delete the item.

				
					## set delete protection to false
item.protect(enable=False)
print(item.protected)
				
			

Refresh the Settings tab.

Content Status

There is an alternate way to set Delete Protection and to define if the ArcGIS Online content item is authoritative or deprecated which can be useful for defining which version of data to use in ArcGIS Online.

The current status of or content item is set to None, neither Authoritative or Deprecated has been set. Let’s set to Authoritative. 

				
					## set content item to authoritative
item.content_status = "authoritative"
				
			
We can see that Delete Protection was also set when updated the Content Status. We can check the Item object protected property and once a Content Status has been set a contentStatus property gets added to the Item object.
				
					print(item.protected)
>>> True

print(item.contentStatus)
>>> org_authoritative
				
			

We will now set the Content Status to Deprecated.

				
					## set content item to deprecated
item.content_status = "deprecated"
				
			

To note, setting as Deprecated does not remove the Delete Protection.

				
					print(item.protected)
>>> True

print(item.contentStatus)
>>> deprecated
				
			

Setting the Content Status to None also does not remove the Delete Protection.

				
					## set content item to None
item.content_status = None
				
			
A Content Status of None will also remove the contentStatus property from the Item object.
				
					print(item.contentStatus)
>>> AttributeError: 'Item' object has no attribute 'contentStatus'
				
			

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

Leave a Comment

Your email address will not be published. Required fields are marked *