<?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 Re: Removing empty feature classes from a geodatabase in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236329#M18388</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Hydronaut, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What specific problem do you have with the code in the link? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I believe that with some of the changes suggested in the comments in the link you sent, this should work. I was removing empty layers from mxd�??s with a similar method. What I found (in v.10) is that you need to make an integer of the results object from GetCount_management. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So if you look at comment #5 in the link, which says:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;result = arcpy.GetCount_management(fc)
print result
if result == 0:
print fc + �?� is empty.�?�
arcpy.delete_management(fc)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Change that to &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;result =&amp;nbsp; arcpy.GetCount_management(lyr)
count = int(result.getOutput(0))
if count == 0:
 arcpy.mapping.RemoveLayer(df, lyr)
 print "Removing " + lyr
else:
 print "Keeping " + lyr
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the code above, substitute arcpy.mapping.RemoveLayer for arcpy.Delete_management. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, you might need to handle FC's inside feature datasets like the first comment suggests.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 11:55:01 GMT</pubDate>
    <dc:creator>StefanHaglund1</dc:creator>
    <dc:date>2021-12-11T11:55:01Z</dc:date>
    <item>
      <title>Removing empty feature classes from a geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236328#M18387</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does anybody know of a script that exists to do this?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have come across &lt;/SPAN&gt;&lt;A href="http://www.aubreyrhea.net/gis/index.php/2009/07/removing-empty-feature-classes-with-python/"&gt;this link&lt;/A&gt;&lt;SPAN&gt; where some people have had success, others not.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried it myself, but no luck.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Feb 2011 00:15:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236328#M18387</guid>
      <dc:creator>JustinOdell</dc:creator>
      <dc:date>2011-02-11T00:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Removing empty feature classes from a geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236329#M18388</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Hydronaut, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What specific problem do you have with the code in the link? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I believe that with some of the changes suggested in the comments in the link you sent, this should work. I was removing empty layers from mxd�??s with a similar method. What I found (in v.10) is that you need to make an integer of the results object from GetCount_management. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So if you look at comment #5 in the link, which says:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;result = arcpy.GetCount_management(fc)
print result
if result == 0:
print fc + �?� is empty.�?�
arcpy.delete_management(fc)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Change that to &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;result =&amp;nbsp; arcpy.GetCount_management(lyr)
count = int(result.getOutput(0))
if count == 0:
 arcpy.mapping.RemoveLayer(df, lyr)
 print "Removing " + lyr
else:
 print "Keeping " + lyr
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the code above, substitute arcpy.mapping.RemoveLayer for arcpy.Delete_management. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, you might need to handle FC's inside feature datasets like the first comment suggests.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:55:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236329#M18388</guid>
      <dc:creator>StefanHaglund1</dc:creator>
      <dc:date>2021-12-11T11:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: Removing empty feature classes from a geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236330#M18389</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Adding to my post above, there's an explanation in the help why you need to handle the result object.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Have a look here: &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000n000000.htm"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000n000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the note under the "Getting results from a tool" heading, it says:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The result object's getOutput method returns a Unicode string from result objects that have output values. This is an important consideration when running a tool such as GetCount, which provides a count of records in a table, or CalculateDefaultClusterTolerance, which provides a cluster tolerance value. To convert the value to the expected type, it will have to be converted from a unicode string using built-in Python functions such as int() or float().&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Feb 2011 13:46:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236330#M18389</guid>
      <dc:creator>StefanHaglund1</dc:creator>
      <dc:date>2011-02-11T13:46:47Z</dc:date>
    </item>
    <item>
      <title>Re: Removing empty feature classes from a geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236331#M18390</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is the code I have.&amp;nbsp; I've never taken a Python scripting course, so I'm a bit of a noob when it comes to programming.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The problem I find when running the script in IDLE, is that it gets stuck in the while loop, and (I think) tries to keep removing the same feature dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;##############################################################
## Delete Empty Feature Classes from Multiple Geodatabases
##
## This script loops through all geodatabases within a directory
## and removes empty featureclasses from each geodatabase.
##
## User must hardcode directory. Run this script in IDLE.
##
## Python Version 2.4
##############################################################

# Create the Geoprocessor object
import arcgisscripting, os, sys, string
gp = arcgisscripting.create()

# Load required toolboxes�?�
gp.AddToolbox("C:\\Program Files\\ArcGIS\\ArcToolbox\\Toolboxes\\Conversion Tools.tbx")

# Set workspace
gp.workspace = "L:\\Justin\\00_jobs\\Python_Scripting_Test\\"

# list personal geodatabases in the current workspace
listmdb = gp.listworkspaces("*", "FileGDB")
mdb = listmdb.next()

# loop through the personal geodatabases
while mdb:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print mdb
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.workspace = mdb
&amp;nbsp;&amp;nbsp;&amp;nbsp; # list feature datasets in personal geodatabase
&amp;nbsp;&amp;nbsp;&amp;nbsp; listfdset=gp.listdatasets()
&amp;nbsp;&amp;nbsp;&amp;nbsp; fdset=listfdset.next()

&amp;nbsp;&amp;nbsp;&amp;nbsp; # loop through feature dataset
&amp;nbsp;&amp;nbsp;&amp;nbsp; while fdset:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "fdset: ", fdset

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not fdset == None:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.workspace = mdb + "\\" + fdset
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # list feature classes in the personal geodatabase
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fcs = gp.listfeatureclasses()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fcs.reset()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = fcs.next()

&amp;nbsp;&amp;nbsp;&amp;nbsp; # loop through the featureclasses
&amp;nbsp;&amp;nbsp;&amp;nbsp; while fc:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print fc, gp.GetCount_management(fc)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # if the feature class table has no records, delete the featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if gp.GetCount_management(fc) == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete_management(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print fc + "deleted from" + mdb
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = fcs.next()

&amp;nbsp;&amp;nbsp;&amp;nbsp; fdset=listfdset.next()

&amp;nbsp;&amp;nbsp;&amp;nbsp; mdb = listmdb.next()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help is greatly appreciated.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:55:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236331#M18390</guid>
      <dc:creator>JustinOdell</dc:creator>
      <dc:date>2021-12-11T11:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: Removing empty feature classes from a geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236332#M18391</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;My advice is: Don't loop with while! (Not in arcpy, and not in previous versions of arcgis either). The ArcGIS sample code is written by someone not knowing python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy version:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
listmdb = arcpy.ListWorkspaces('*', 'FileGDB')
for mdb in listmdb:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print mdb
&amp;nbsp;&amp;nbsp;&amp;nbsp; # same logic as above
&amp;nbsp;&amp;nbsp;&amp;nbsp; # change inner loops to for loops too
&amp;nbsp;&amp;nbsp;&amp;nbsp; # don't call next, since it's done behind the scene with the for loop
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ArcGIS 9.2 version:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
listmdb = gp.ListWorkspaces('*', 'FileGDB')
for mdb in iter(listmdb.next, None): # Note: no function parantheses following next
&amp;nbsp;&amp;nbsp;&amp;nbsp; print mdb
&amp;nbsp;&amp;nbsp;&amp;nbsp; # same logic as above
&amp;nbsp;&amp;nbsp;&amp;nbsp; # change inner loops to for loops too
&amp;nbsp;&amp;nbsp;&amp;nbsp; # don't call next, since it's done behind the scene with the for loop
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 11:55:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236332#M18391</guid>
      <dc:creator>NiklasNorrthon</dc:creator>
      <dc:date>2021-12-11T11:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Removing empty feature classes from a geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236333#M18392</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hmm... I seem to be making less progress than with the while loops.&amp;nbsp; I will get myself in to do a Python course, but as it stands I am struggling to get the code to do what I want it to.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is anybody able to write or test the code and fix it so that it deletes empty feature classes AND empty feature datasets from a geodatabase?&amp;nbsp; I would be very thankful.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Feb 2011 22:11:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/removing-empty-feature-classes-from-a-geodatabase/m-p/236333#M18392</guid>
      <dc:creator>JustinOdell</dc:creator>
      <dc:date>2011-02-14T22:11:59Z</dc:date>
    </item>
  </channel>
</rss>

