Cannot Delete Locked .tif.aux.xml File Generated by ApplySymbologyFromLayer

204
6
a week ago
WentaoChe
Frequent Contributor

After using the ArcGIS Pro tool arcpy.management.ApplySymbologyFromLayer() to apply symbology to a .tif raster file, a corresponding .tif.aux.xml file is automatically generated. However, I'm unable to delete this .tif.aux.xml file because it appears locked.  I tried os.remove, but it does not work. I am using ArcGIS Pro 3.5. Is there a way to delete this xml file?

Here are the code lines:

import arcpy
import os, glob

# DEM_tif_Folder = arcpy.GetParameterAsText(0)

DEM_tif_Folder = r'D:\20250305-CS-Map-Tool\DEM_tif_Folder'

cs_dem_lyrx = os.path.join(DEM_tif_Folder, 'DEM_Sample.tif.lyrx')

os.chdir(DEM_tif_Folder)
arcpy.env.workspace = DEM_tif_Folder

arcpy.env.overwriteOutput = True
arcpy.env.pyramid = None
arcpy.env.rasterStatistics = None

[os.remove(x) for x in arcpy.ListFiles('*.xml')]

aprx = arcpy.mp.ArcGISProject('current')

m = aprx.listMaps()[0]

for lyr in m.listLayers():
  m.removeLayer(lyr)

tif = 'DEM2356.tif'

full_tif = os.path.join(DEM_tif_Folder, tif)
m.addDataFromPath(full_tif)
tif_layer = m.listLayers()[0]
results = arcpy.management.ApplySymbologyFromLayer(tif_layer, cs_dem_lyrx)

out_aprx = DEM_tif_Folder + os.sep + 'tmp_dem_tif.aprx'
aprx.saveACopy(out_aprx)
 
for lyr in m.listLayers():
  m.removeLayer(lyr)
  
aprx.deleteItem(aprx.listMaps()[0])

del aprx
del m

# time.sleep(9)

xml_file = tif + '.aux.xml'
if os.path.exists(DEM_tif_Folder + os.sep + xml_file):
  os.remove(xml_file)

for file in os.listdir():
  if file.endswith('.tif.aux.xml'):
    arcpy.AddMessage('    %s: ' % file)
    os.remove(file)

os.system('del *.tif.aux.xml')

Here are the contents of DEM2356.tif.aux.xml:

<PAMDataset>
  <PAMRasterBand band="1">
    <Histograms>
      <HistItem>
        <HistMin>159.64497375488</HistMin>
        <HistMax>357.10797119141</HistMax>
        <BucketCount>256</BucketCount>
        <IncludeOutOfRange>1</IncludeOutOfRange>
        <Approximate>0</Approximate>
        <HistCounts>34|190|325|362|425|488|580|761|743|820|877|908|1223|1308|1309|1361|1560|1606|1709|1673|1834|1859|1931|1981|2011|2083|2105|2232|2196|2271|2419|2487|2480|2468|2500|2601|2653|2712|2844|2932|2903|2931|3043|3037|3116|3100|3134|3194|3278|3295|3319|3392|3464|3532|3674|3685|3685|3670|3718|3812|3870|3877|3920|3969|4074|4189|4182|4365|4491|4377|4481|4582|4706|4672|4706|4782|4802|4801|4879|4866|4853|4876|5000|5057|5044|5125|5119|5161|5195|5296|5333|5350|5386|5534|5568|5625|5728|5710|5749|5775|5806|5771|5829|5927|6003|6025|6113|6071|6120|6053|6117|6156|6162|6139|6173|6069|6089|6119|6156|6147|6157|6226|6314|6238|6319|6328|6299|6244|6288|6313|6202|6251|6213|6258|6291|6349|6341|6345|6240|6179|6191|6183|6094|6308|6204|6218|6165|6286|6120|6051|6037|5956|5783|5760|5776|5752|5670|5634|5704|5473|5462|5353|5247|5298|5261|5161|5243|5187|5117|5121|5308|5283|5380|5201|4811|4529|4462|4428|4460|4284|4306|4178|4035|4068|3970|3840|3654|3663|3690|3580|3558|3288|3224|3375|3595|3218|3571|3209|3359|2978|3106|2987|2993|2903|2766|2502|2441|2310|2142|1958|1887|1918|2021|2228|2329|2308|2220|1939|2157|2094|1528|1398|1308|1319|1239|1073|1080|1023|1059|1081|1137|1156|692|741|629|563|521|503|461|443|419|375|349|321|314|275|216|134|124|126|127|110|108|93|67|1</HistCounts>
      </HistItem>
    </Histograms>
  </PAMRasterBand>
</PAMDataset>

 

0 Kudos
6 Replies
JonM32
by
Frequent Contributor

@WentaoChe Maybe the raster needs that file for display since it looks like a statistics file that holds information needed for symbology. So by default, Pro won't let you delete it?

 

Also, you could always try finding the file in the file explorer and try manually deleting it.

Jon
0 Kudos
WentaoChe
Frequent Contributor

@JonM32 Thank you very much for your reply. 

After using ApplySymbologyFromLayer(), I add a contour shapefile on top of the DEM .tif and then use exportToPNG to export the final map. At that point, my processing is complete, and there is no need to retain the .xml statistics file used for symbology. Since I have thousands of .tif files to process, I need an automated way to remove these .xml files rather than manually deleting them.

Although I could use another tool to remove them afterward, it feels inefficient. Is there a better approach within ArcGIS Pro to handle this automatically?

0 Kudos
JonM32
by
Frequent Contributor

@WentaoChe Anytime - in terms of a better approach: I can't speak to an automated solution to delete them but in terms of troubling shooting I would try the ideas below.

  • Try this process on an older version of Pro (3.4 or 3.3) and see if it occurs still. Maybe it's a bug in 3.5?
  • Try this process on different tifs to see if it's isolated to one specific DEM or all of them
Jon
0 Kudos
WentaoChe
Frequent Contributor

@JonM32 thank you very much for your reply.

1. I tried it by ArcGIS 3.4. .tif.aux.xml file could not be removed. 

2. I have tried 100 tif files, all the 100 xml file could not be removed.

Thanks again.

Luke_Pinner
MVP Regular Contributor

The results object or tif_layer vars may be holding a reference the dataset causing a lock on the xml.  Try cleaning those up (del results etc)

0 Kudos
WentaoChe
Frequent Contributor

@Luke_Pinner , Thank you very much for your reply. 

I tried deleting the results object and tif_layer variables, but the .tif.aux.xml file is still locked and could not be removed using os.remove(xml_file).

0 Kudos