Slower for large datasets; does not handle attachments as reliably as the export method.
import arcpy # The REST URL of the hosted layer (found in Item Details) service_url = "https://arcgis.com" output_gdb = r"C:\Data\LocalStore.gdb" # Directly copy the features arcpy.conversion.FeatureClassToFeatureClass(service_url, output_gdb, "Downloaded_Layer") Use code with caution. arcpy download hosted feature layer
from arcgis.gis import GIS import os # Connect to ArcGIS Online or Enterprise gis = GIS("https://www.arcgis.com", "Your_Username", "Your_Password") # Find the hosted feature layer by Item ID item_id = "your_item_id_here" feature_layer_item = gis.content.get(item_id) # Export to File Geodatabase format on the server # This creates a NEW item in your AGOL 'My Content' print(f"Exporting {feature_layer_item.title}...") export_item = feature_layer_item.export(f"Export_{feature_layer_item.title}", "File Geodatabase") # Download the exported .zip file to a local path download_path = r"C:\Data\Backups" export_item.download(save_path=download_path) # Clean up the temporary export item from AGOL to save credits/space export_item.delete() print("Download complete.") Use code with caution. Slower for large datasets; does not handle attachments