<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Help with Notebook to find empty or corrupt views in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/help-with-notebook-to-find-empty-or-corrupt-views/m-p/1667828#M67179</link>
    <description>&lt;P&gt;Hello community (cc &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/50014"&gt;@David_McRitchie&lt;/a&gt;),&lt;BR /&gt;I've encountered an issue whereby when deleting unused fields from a hosted feature layer, some of the associated views are becoming corrupted. After some discussions with ESRI support, it was suggested that one way to improve processes to allow us to identify and fix any broken views, is after any field deletions, to run a notebook that does the following:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Find 'view' layers in ArcGIS Online&lt;/LI&gt;&lt;LI&gt;From the view layers, identify any that have &amp;lt;10 rows of data or are 'empty' (contain data but not responsive)&lt;/LI&gt;&lt;LI&gt;Output the view layers as a csv&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I have trialled the following code, however it returns no results ("No empty hosted view layers found.&amp;nbsp;Results saved to empty_view_layers.txt"). I know however that there are multiple view layers with less than 10 rows of data but none are being picked up. I'm not quite sure what is going wrong here so any suggestions would be gratefully received.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS # import necessary libraries
gis = GIS("home") # connect to your GIS

# Function to find empty hosted view layers
def find_empty_view_layers():
    empty_view_layers = []
    # Search for hosted feature services
    for item in gis.content.search(query="type:Feature Service", max_items=10000):
        if "View Layer" in item.typeKeywords:  # Check if the item is a view layer
            # Check if the view layer is empty
            feature_count = item.layers[0].query(return_count_only=True) if item.layers else 0
            if feature_count &amp;lt;= 10:
                empty_view_layers.append(item.title)
    return empty_view_layers

# Retrieve and display the empty view layers
empty_layers = find_empty_view_layers()
if empty_layers:
    print("Empty Hosted View Layers:")
    for layer in empty_layers:
        print(f"- {layer}")
else:
    print("No empty hosted view layers found.")

# Optionally, save the results to a text file
with open("empty_view_layers.txt", "w") as file:
    for layer in empty_layers:
        file.write(layer + "\n")
print("Results saved to empty_view_layers.txt")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Alternatively, a method for outputting all view layers and their associated feature count would also do the job. I have tried this via the following approach:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS # import libraries
gis = GIS("home") # connect to your GIS

items = gis.content.search(query='View Service', max_items=10000) # get view layers
for layer in items: # check the output by printing the view layer titles
    print(layer.title)

# Loop through the layers and get the feature count
for layer_item in items:
    print(f"Layer: {layer_item.title}")
    for lyr in layer_item.layers:
        feature_count = lyr.query(where="1=1", return_count_only=True)
        print(f"  Feature Layer: {lyr.properties.name}, Feature Count: {feature_count}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This 2nd approach works to begin with, but then returns the error below. This appears to be caused by it trying to work against a geocoding view. Therefore, is there a way to limit the layer list to only '&lt;SPAN&gt;hosted, view' type items, or at least exclude a geocoding view?&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, k)
  14045             if not self._hydrated and not k.startswith("_"):
  14046                 self._hydrate()
&amp;gt; 14047             return dict.__getitem__(self, k)

KeyError: 'layers'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_142/3627018840.py in ?()
      1 # Loop through the layers and get the feature count
      2 for layer_item in items:
      3     print(f"Layer: {layer_item.title}")
----&amp;gt; 4     for lyr in layer_item.layers:
      5         feature_count = lyr.query(where="1=1", return_count_only=True)
      6         print(f"  Feature Layer: {lyr.properties.name}, Feature Count: {feature_count}")

/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, name)
  13999     def __getattribute__(self, name):
  14000         if name == "layers":
&amp;gt; 14001             if self["layers"] is None or self["layers"] == []:
  14002                 try:
  14003                     with _common_utils._DisableLogger():
  14004                         self._populate_layers()

/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, k)
  14043             return dict.__getitem__(self, k)
  14044         except KeyError:
  14045             if not self._hydrated and not k.startswith("_"):
  14046                 self._hydrate()
&amp;gt; 14047             return dict.__getitem__(self, k)

KeyError: 'layers'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Thanks in advance for any assistance on this.&lt;/P&gt;</description>
    <pubDate>Fri, 21 Nov 2025 11:28:25 GMT</pubDate>
    <dc:creator>DataOfficer</dc:creator>
    <dc:date>2025-11-21T11:28:25Z</dc:date>
    <item>
      <title>Help with Notebook to find empty or corrupt views</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/help-with-notebook-to-find-empty-or-corrupt-views/m-p/1667828#M67179</link>
      <description>&lt;P&gt;Hello community (cc &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/50014"&gt;@David_McRitchie&lt;/a&gt;),&lt;BR /&gt;I've encountered an issue whereby when deleting unused fields from a hosted feature layer, some of the associated views are becoming corrupted. After some discussions with ESRI support, it was suggested that one way to improve processes to allow us to identify and fix any broken views, is after any field deletions, to run a notebook that does the following:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Find 'view' layers in ArcGIS Online&lt;/LI&gt;&lt;LI&gt;From the view layers, identify any that have &amp;lt;10 rows of data or are 'empty' (contain data but not responsive)&lt;/LI&gt;&lt;LI&gt;Output the view layers as a csv&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I have trialled the following code, however it returns no results ("No empty hosted view layers found.&amp;nbsp;Results saved to empty_view_layers.txt"). I know however that there are multiple view layers with less than 10 rows of data but none are being picked up. I'm not quite sure what is going wrong here so any suggestions would be gratefully received.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS # import necessary libraries
gis = GIS("home") # connect to your GIS

# Function to find empty hosted view layers
def find_empty_view_layers():
    empty_view_layers = []
    # Search for hosted feature services
    for item in gis.content.search(query="type:Feature Service", max_items=10000):
        if "View Layer" in item.typeKeywords:  # Check if the item is a view layer
            # Check if the view layer is empty
            feature_count = item.layers[0].query(return_count_only=True) if item.layers else 0
            if feature_count &amp;lt;= 10:
                empty_view_layers.append(item.title)
    return empty_view_layers

# Retrieve and display the empty view layers
empty_layers = find_empty_view_layers()
if empty_layers:
    print("Empty Hosted View Layers:")
    for layer in empty_layers:
        print(f"- {layer}")
else:
    print("No empty hosted view layers found.")

# Optionally, save the results to a text file
with open("empty_view_layers.txt", "w") as file:
    for layer in empty_layers:
        file.write(layer + "\n")
print("Results saved to empty_view_layers.txt")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;Alternatively, a method for outputting all view layers and their associated feature count would also do the job. I have tried this via the following approach:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS # import libraries
gis = GIS("home") # connect to your GIS

items = gis.content.search(query='View Service', max_items=10000) # get view layers
for layer in items: # check the output by printing the view layer titles
    print(layer.title)

# Loop through the layers and get the feature count
for layer_item in items:
    print(f"Layer: {layer_item.title}")
    for lyr in layer_item.layers:
        feature_count = lyr.query(where="1=1", return_count_only=True)
        print(f"  Feature Layer: {lyr.properties.name}, Feature Count: {feature_count}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This 2nd approach works to begin with, but then returns the error below. This appears to be caused by it trying to work against a geocoding view. Therefore, is there a way to limit the layer list to only '&lt;SPAN&gt;hosted, view' type items, or at least exclude a geocoding view?&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, k)
  14045             if not self._hydrated and not k.startswith("_"):
  14046                 self._hydrate()
&amp;gt; 14047             return dict.__getitem__(self, k)

KeyError: 'layers'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_142/3627018840.py in ?()
      1 # Loop through the layers and get the feature count
      2 for layer_item in items:
      3     print(f"Layer: {layer_item.title}")
----&amp;gt; 4     for lyr in layer_item.layers:
      5         feature_count = lyr.query(where="1=1", return_count_only=True)
      6         print(f"  Feature Layer: {lyr.properties.name}, Feature Count: {feature_count}")

/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, name)
  13999     def __getattribute__(self, name):
  14000         if name == "layers":
&amp;gt; 14001             if self["layers"] is None or self["layers"] == []:
  14002                 try:
  14003                     with _common_utils._DisableLogger():
  14004                         self._populate_layers()

/opt/conda/lib/python3.11/site-packages/arcgis/gis/__init__.py in ?(self, k)
  14043             return dict.__getitem__(self, k)
  14044         except KeyError:
  14045             if not self._hydrated and not k.startswith("_"):
  14046                 self._hydrate()
&amp;gt; 14047             return dict.__getitem__(self, k)

KeyError: 'layers'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Thanks in advance for any assistance on this.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Nov 2025 11:28:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/help-with-notebook-to-find-empty-or-corrupt-views/m-p/1667828#M67179</guid>
      <dc:creator>DataOfficer</dc:creator>
      <dc:date>2025-11-21T11:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Notebook to find empty or corrupt views</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/help-with-notebook-to-find-empty-or-corrupt-views/m-p/1674728#M67469</link>
      <description>&lt;P&gt;Apologies this had gotten buried in my email box. Is this still an ongoing issue?&lt;/P&gt;&lt;P&gt;Taking a look it is a good question. I think we are best focusing on the second method you have, and getting our returned list of content filtered to just Feature Layers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you use the following code adjustment does this function better?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS # import libraries
gis = GIS("home") # connect to your GIS

items = gis.content.search(query='View Service', item_type='Feature Service',  max_items=10000) # get view layers
for layer in items: # check the output by printing the view layer titles
    print(layer.title)

# Loop through the layers and get the feature count
for layer_item in items:
    print(f"Layer: {layer_item.title}")
    for lyr in layer_item.layers:
        feature_count = lyr.query(where="1=1", return_count_only=True)
        print(f"  Feature Layer: {lyr.properties.name}, Feature Count: {feature_count}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If not then please let me know and we can review early in the new year. Hope you enjoy the holidays!&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Tue, 23 Dec 2025 17:11:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/help-with-notebook-to-find-empty-or-corrupt-views/m-p/1674728#M67469</guid>
      <dc:creator>David_McRitchie</dc:creator>
      <dc:date>2025-12-23T17:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Notebook to find empty or corrupt views</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/help-with-notebook-to-find-empty-or-corrupt-views/m-p/1676396#M67542</link>
      <description>&lt;P&gt;Hi David,&lt;BR /&gt;&lt;BR /&gt;Thanks for the assistance. That amendment does seem to resolve things. Appreciate you getting back to me.&lt;BR /&gt;&lt;BR /&gt;Here is the final script in case of use to anyone else.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
gis = GIS("home")
import csv

items = gis.content.search(query='View Service', item_type='Feature Service',  max_items=10000) # get view layers
for layer in items: # check the output by printing the view layer titles
    print(layer.title)

# Open a CSV file to write the output
with open('output.csv', mode='w', newline='', encoding='utf-8') as file:
    writer = csv.writer(file)
    # Write the header row
    writer.writerow(['Item Title', 'Item Name','Feature Count'])

    # Loop through the layers and get the feature count
    for layer_item in items:
        for lyr in layer_item.layers:
            feature_count = lyr.query(where="1=1", return_count_only=True)
            # Write the item title and feature count to the CSV file
            writer.writerow([layer_item.title, lyr.properties.name, feature_count])

print("Output saved to 'output.csv'")&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jan 2026 17:19:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/help-with-notebook-to-find-empty-or-corrupt-views/m-p/1676396#M67542</guid>
      <dc:creator>DataOfficer</dc:creator>
      <dc:date>2026-01-07T17:19:32Z</dc:date>
    </item>
  </channel>
</rss>

