<?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: Keeping Object ID field only and deleting any other field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436311#M34303</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Instead of trying to guess ahead of time which fields you can and can't delete from various data sources, just wrap your code in Python try/expect block:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcgisscripting

fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# path to feature class&lt;/SPAN&gt;
desc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;Describe&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; fld &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; desc&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;fields&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;try&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
      arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;DeleteField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fld&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;name&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;except&lt;/SPAN&gt; arcgisscripting&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ExecuteError &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; e&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
       &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Could not delete: {}"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fld&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;name&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 19:33:17 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2021-12-11T19:33:17Z</dc:date>
    <item>
      <title>Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436302#M34294</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Pretty straight forward. I am building a tool that outputs an Excel file with XYZ data pertaining to a polyline route. Parameters include:&lt;/P&gt;&lt;P&gt;Input Points (points have been generated along a line fc every 1 meter)&lt;/P&gt;&lt;P&gt;Input DEM&lt;/P&gt;&lt;P&gt;Output Location for Excel sheet&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The tool takes the input points, make a copy, SUPPOSED TO DELETE ALL FIELDS EXCEPT OBJECT ID, uses Add XY tool, then uses Extract Value to get the elevation, and finally exports the table to an Excel file. So the issue is when i am trying to delete all the fields except Object ID. All i get is a runtime error: error executing tool. I have tried many versions of my code to delete the fields. Here is one example:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;SaveList 			= ["OBJECTID", "OBJECT_ID", "ObjectID"]

for feild in arcpy.ListFields(Copied_Points):
	if feild.name not in SaveList:
		arcpy.DeleteField_management(Copied_Points, feild)
del field‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any thoughts on how to make this work? Thanks guys/gals.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:33:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436302#M34294</guid>
      <dc:creator>ConnorMcivor</dc:creator>
      <dc:date>2021-12-11T19:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436303#M34295</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have an issue with getting the field name&amp;nbsp;&lt;/P&gt;&lt;P&gt;OBJECTID is for file geodatabases&lt;/P&gt;&lt;P&gt;otherwise you might want to use a 'describe' object and get the id from there&lt;/P&gt;&lt;P&gt;&lt;A class="link-titled" href="https://pro.arcgis.com/en/pro-app/arcpy/functions/table-properties.htm" title="https://pro.arcgis.com/en/pro-app/arcpy/functions/table-properties.htm"&gt;Table properties—ArcPy Functions | ArcGIS Desktop&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:04:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436303#M34295</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2019-08-29T18:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436304#M34296</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Since you are using &lt;A href="https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/listfields.htm"&gt;ListFields&lt;/A&gt;&amp;nbsp;you can also try:&lt;/P&gt;&lt;PRE class="language-none line-numbers"&gt;&lt;CODE&gt;     if feild.type != "OID" :&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Do you also wish to delete any geometry?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:30:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436304#M34296</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2019-08-29T18:30:32Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436305#M34297</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ok so yes I only work with file geodatabases when working on anything related to custom built tools. I am aware that the OID and Shape fields are required. I just need to strip away any tabular data and keep the essentials. Did I miss something in what you had suggested?&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:32:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436305#M34297</guid>
      <dc:creator>ConnorMcivor</dc:creator>
      <dc:date>2019-08-29T18:32:02Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436306#M34298</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I just need the required fields for file geodatabases. All non-required fields need to be gone. I now realise that i cannot delete Shape either and that is fine. So keep OID and SHAPE fields. All else needs to be removed. I dont want to hard code it because the input will not be the same fields each time.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:34:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436306#M34298</guid>
      <dc:creator>ConnorMcivor</dc:creator>
      <dc:date>2019-08-29T18:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436307#M34299</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If ListFields is indicating a type of OID, then use the name property to delete it.&amp;nbsp;To skip both object ID&amp;nbsp; and geometry fields in your deletion, you might try:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;field_types = ['OID', 'GEOMETRY'] # field types to ignore

#...
for feild in arcpy.ListFields(Copied_Points): # note spelling of "feild"
    if feild.type.upper() not in field_types:
        # then delete field.name‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:33:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436307#M34299</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2021-12-11T19:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436308#M34300</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;There is also the .required property.&amp;nbsp; I haven't tested it, but it probably returns a True/False. This would be a more generic test.&lt;/P&gt;&lt;PRE class="language-none line-numbers"&gt;&lt;CODE&gt;    if feild.required is false: # perhaps: == 'False'‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:54:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436308#M34300</guid>
      <dc:creator>RandyBurton</dc:creator>
      <dc:date>2019-08-29T18:54:55Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436309#M34301</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The problem with "required" fields is that non-system fields can be required if configured that way when created.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2019 22:51:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436309#M34301</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2019-08-29T22:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436310#M34302</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks everyone for your input... I used a combination of a few of the things suggested here and added other elements in to the mix. I am now trying to make it so that the user can CHOOSE feilds that they want to keep in the output file. An example of the code now:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;Copy_KP = arcpy.GetParameterAsText(#)&lt;/P&gt;&lt;P&gt;Keep_Fields = arcpy.GetParameterAsText(#) &amp;nbsp;&amp;nbsp;&amp;nbsp;# Feild parameter, multivalue, obtained from original data source&lt;/P&gt;&lt;P&gt;keepList&lt;SPAN&gt; &lt;/SPAN&gt;= []&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for item in Keep_Fields:&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;keepList.append(item)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;fieldNames = [f.name for f in arcpy.Describe(Copy_KP).Fields if not (f.type in ["OID", "Geometry"] or f.name in ["Shape_Length", "Shape_Area"] or f.name.upper() in keepList )] &lt;BR /&gt;if fieldNames: &lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arcpy.DeleteField_management(Copy_KP, fieldNames)&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I set the "Keep_Fields" parameter as "Field" type with multivalue enabled. The Keep_Fields part isn't working yet but the fields all delete except the "OID". So at least there is that. I will keep working at it next week.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Again, thanks everyone for the help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Aug 2019 23:07:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436310#M34302</guid>
      <dc:creator>ConnorMcivor</dc:creator>
      <dc:date>2019-08-29T23:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: Keeping Object ID field only and deleting any other field</title>
      <link>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436311#M34303</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Instead of trying to guess ahead of time which fields you can and can't delete from various data sources, just wrap your code in Python try/expect block:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy
&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcgisscripting

fc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="comment token"&gt;# path to feature class&lt;/SPAN&gt;
desc &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;Describe&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; fld &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; desc&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;fields&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;try&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
      arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;DeleteField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; fld&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;name&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
  &lt;SPAN class="keyword token"&gt;except&lt;/SPAN&gt; arcgisscripting&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;ExecuteError &lt;SPAN class="keyword token"&gt;as&lt;/SPAN&gt; e&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
       &lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Could not delete: {}"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fld&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;name&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:33:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/keeping-object-id-field-only-and-deleting-any/m-p/436311#M34303</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-11T19:33:17Z</dc:date>
    </item>
  </channel>
</rss>

