PyQGIS - remove layer from legend
I think this function will work (for QGIS version >= 3.0) :
def rmvLyr(lyrname):
qinst = QgsProject.instance()
qinst.removeMapLayer(qinst.mapLayersByName(lyrname)[0].id())
Call it by :
for lyr in ['Patient Data', 'Google Maps']:
rmvLyr(lyr)
You can try this:
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "Patient Data":
QgsMapLayerRegistry.instance().removeMapLayers([lyr.id()])