5

This question already has an answer here:

Considering my program is generating a shapefile that has to be added to the "Layers" tab in QGIS interface. However, once the utility of the layer is completed, I need to remove the layer in the middle of the code. I want to remove only the active layer and not all.

To put it in easier words, is there an opposite to self.iface.addVectorLayer() ?

marked as duplicate by Chad Cooper, R.K., Jason Scheirer, SS_Rebelious, Mapperz Jan 19 '15 at 16:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Thanks @Akhil but your question may be seen as a duplicate if the solution in the referenced post worked for you. I will post it and let the moderators decide. – Joseph Jan 19 '15 at 11:17
  • 1
    Absolutely. Do the needful, please. This could however assist others when they use a different set of search keywords, but it is a duplicate. – Akhil Jan 19 '15 at 11:24
8

This question may be seen as a duplicate with this post, in which case I will happily remove my answer.

The solution, provided by @andytilia, involves using the following command for versions of QGIS from 1.8 to 2.99.

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

The solution involves using the following command for QGIS 3.X.

QgsProject.instance().removeMapLayers( [vl.id()] )
  • 1
    It's actually the opposite. From the docs: "This method is deprecated since QGIS 1.8, you should use removeMapLayers rather." – Germán Carrillo Jan 19 '15 at 13:09
  • This command works if you have add the layer. But I have a problem with vector layer defined and not loaded in the project which not allow the deletion of the temporary folder of my QGIS plugin – Roberto Marzocchi Jun 5 at 10:50

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