13

I managed to add QgsVectorLayer in QGIS through a python plugin, for example:

vl = self.iface.addVectorLayer(uri.uri(), layerName, self.dbConn.getProviderName())

But how can I remove it?

  • Didn't know that you could actually use this method in a variable assignment. Helped me solve a problem that I didn't know I had :P – Gob Tron Jun 23 '17 at 19:11
15

Are you on QGIS 1.8? If so, you can remove your vl with:

QgsMapLayerRegistry.instance().removeMapLayers( [vl.id()] )

otherwise:

QgsMapLayerRegistry.instance().removeMapLayer( vl.id() )

And if you are on QGIS 3.0+, replace QgsMapLayerRegistry with QgsProject.

Or if by chance you want to remove all empty layers, check out the Remove Empty Layers plugin.

  • ok -- note that I just edited the answer to use your vl variable. – andytilia Nov 27 '12 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.