# removeLayers.py
# Purpose: Remove the first layer in the table of contents.
# Input: No arguments required.

import arcpy, os

# Get a MapDocument object.
aprxName = 'layerManipExample2.aprx'
mapPath = 'C:/gispy/data/ch24/maps/layerManipExample2/'
aprx = arcpy.mp.ArcGISProject(mapPath + aprxName)

# Get a list of the Map objects.
maps = aprx.listMaps()

# Get the first Map object.
map = maps[0]

# Get a list of Layer objects in this data frame.
lyrs = map.listLayers('*')

# Get the first Layer object.
layerToRemove = lyrs[0]

# Remove the layer.
map.removeLayer(layerToRemove)

# Save a copy of the map.
copyName = 'C:/gispy/scratch/' + os.path.splitext(aprxName)[0] + '_V2.aprx'
aprx.saveACopy(copyName)
print('{0} created.'.format(copyName))

# Delete the aprx object to release the lock.
del aprx