PROBLEM

Unable to truncate ArcGIS Online hosted layers using PyCharm

Last Published: February 26, 2026

Description

Attempts to truncate an ArcGIS Online hosted layer using a script in PyCharm fail, and the following error message is returned:

Traceback (most recent call last): 
  File "C:\Users\<Username>\PycharmProjects\PythonProject\idle_repro_test2.py", line 1, in <module>        
    import arcpy
ModuleNotFoundError: No module named 'arcpy'
Error message displayed in PyCharm

Cause

The PyCharm project interpreter is set to use a Python environment that is not within the ArcGIS Pro conda environment. ArcPy is only installed  within the ArcGIS Pro conda environment. This is required to run ArcGIS Pro geoprocessing tools such as the Truncate Table tool. Refer to ArcGIS Pro: Python and geoprocessing for more information.

Solution or Workaround

Use IDLE to truncate the hosted layer in ArcGIS Online

Using IDLE prevents a mismatch in the Python environment when running the Truncate Table tool because launching it from the ArcGIS Pro Python Command Prompt activates the ArcGIS Pro Python environment and ensures the scripts are run using the same interpreter.

  1. Copy the URL of the hosted layer from ArcGIS Online.
    Prerequisites for the hosted layer URL:
    • Ensure the hosted layer supports Truncate. 
    • Toggle Enable sync (required for offline use and collaboration) off.
    • Ensure the hosted layer has no attachments. Refer to ArcGIS Pro: Truncate Table (Data Management) for more information.
    1. Open ArcGIS Online.
    2. Navigate to Content > My Content and select the hosted feature layer. In this example, it is TESTING_SUBLAYER_OUT.
    3. In the item details page, scroll down to the URL section and click View. The ArcGIS REST Services Directory page is displayed.
      View icon of the hosted layer URL section
    4. Navigate to Layers and click the sublayer. In this example, layer0_ExportFeatures (0) is selected.
      sublayer with ID 0
    5. At the top of this page, right-click JSON > Copy link address.
    6. Save the URL and ensure it ends with the layer ID. Remove any characters after the ID.

      The URL below is an example of the saved URL.

      Layer with ID:0 URL link
  2. In IDLE Shell, truncate the hosted layer from ArcGIS Online.
    1. Open the ArcGIS Pro Python Command Prompt.
    2. Display and ensure the correct ArcGIS Pro Python interpreter is used.
      python -c "import sys; print(sys.executable)"
    3. Launch IDLE Shell.
      python -m idlelib.idle

      The window below is displayed after IDLE Shell is launched from the Python command prompt.

      IDLE SHELL launched from Python Command Prompt
    4. Click File > New File to open the IDLE Editor window.
      Create New File from the IDEL Shell
    5. In the IDLE Editor window, specify the following Python code to truncate the hosted feature layer in ArcGIS Online. Update the code with the user's portal credentials, portal URL and the hosted layer URL.
      import arcpy
      import datetime
      import sys
      import getpass
      
      def log(msg):
          ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
          print(f"[{ts}] {msg}")
      
      log("=== Truncate Repro Test Started ===")
      log(f"Python executable: {sys.executable}")
      
      try:
          portal_url = "https://www.arcgis.com" #change this part if using organization account
          username = "<portal_username>" 
          password = "<portal_password>"
          arcpy.SignInToPortal(portal_url, username, password)
          log(f"Active portal: {arcpy.GetActivePortalURL()}")
      
          layer0_url = r"<hosted_layer_URL>"
      
          log("Creating feature layer...")
          lyr = arcpy.management.MakeFeatureLayer(layer0_url, "target_lyr").getOutput(0)
          log("MakeFeatureLayer SUCCESS")
      
          log("Running TruncateTable...")
          arcpy.management.TruncateTable(lyr)
          log("TruncateTable SUCCESS")
      
      except Exception as ex:
          log(f"FAILED: {ex}")
      
      finally:
          log("--- ArcPy Messages ---")
          log(arcpy.GetMessages())
          log("=== Truncate Repro Test Completed ===")
      
    6. Click Run > Run Module to run the Python script in IDLE Shell.
      Click Run Module in Run tab
    7. Save and rename the file with a .py extension to save it as a Python file in File Explorer. In this example, it is truncate_test_layer().py.

    The IDLE Shell window below indicates the truncate operation is successful.

    Output display of truncate operation in IDLE Shell

Article ID: 000036798

Software:
  • ArcGIS Pro
  • ArcGIS Online
  • Third Party Product

Get support with AI

Resolve your issue quickly with the Esri Support AI Chatbot.

Start chatting now

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Start chatting now

Go to download options