I am developing a Python plugin for QGIS. In the QGIS map window, certain features of a vector layer are selected and those features are highlighted. Now I need to delete all the existing features from another vector layer without disturbing the current selection in the map window.

Is it possible to delete all the features of a vector layer without selecting them?

  • 2
    Please rephrase your question to indicate where you're getting stuck. Avoid questions that begin with "Is it possible..." as the answer is most likely "Yes". What have you tried so far?
    – Fezter
    Commented Oct 26, 2016 at 5:03

2 Answers 2


You could use the following code which is heavily based on the answer from this post: How to delete selected features in QGIS using Python

layer = iface.activeLayer()
with edit(layer):   
    for feat in layer.getFeatures():


Thanks to @GermánCarrillo, a more efficient method could be to delete all features at once:

layer = iface.activeLayer()
with edit(layer):
    listOfIds = [feat.id() for feat in layer.getFeatures()]
    layer.deleteFeatures( listOfIds )
  • 1
    That works perfectly !!
    – Sjs
    Commented Oct 26, 2016 at 11:50
  • @Sjs - Awesome, glad it worked =)
    – Joseph
    Commented Oct 26, 2016 at 11:50
  • 3
    You could remove all features at once with layer.deleteFeatures( listOfIds ). Commented Oct 26, 2016 at 20:00
  • 1
    @GermánCarrillo - That is indeed a much better approach, many thanks ;)
    – Joseph
    Commented Nov 7, 2016 at 10:08
  • 2
    For Qgis 3.2 instead of layer.deleteFeatures( listOfIds ) works layer.dataProvider().deleteFeatures( listOfIds )
    – Vadym
    Commented Dec 7, 2018 at 19:11

In QGIS 3 one can use the truncate() method of the QgsVectorDataProvider class.

Removes all features from the layer.

This requires either the FastTruncate or DeleteFeatures capability. Providers with the FastTruncate capability will use an optimised method to truncate the layer.

# referring to the original Vector layer
layer = QgsProject.instance().mapLayersByName("points")[0]

# accessing Vector layer provider
provider = source_layer.dataProvider()
# deleting all features in the Vector layer
  • 1
    That's a nice new feature :)
    – Joseph
    Commented May 12, 2017 at 11:59
  • 1
    It is not work in editing mode Commented Apr 8, 2019 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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