I am using QGIS 3.10.2-A Coruña and its Python Console. Currently I am modifying features and attributes of a shape file, e.g. dropping features:
caps = layer.dataProvider().capabilities()
if caps & QgsVectorDataProvider.DeleteFeatures:
res = layer.dataProvider().deleteFeatures(selId)
where selId
are the features to be dropped.
I noticed that when I do such operations changes are immediately saved to the file that is stored on my hard disk, which sort of eliminates the advantage of using a script in the first place.
What do I need to do to get around changes being saved automatically? I read about "editing buffers" in the QGIS Developer Cookbook but I am unsure whether that's what I need.
EDIT:
Just noticed that the layer is only saved on the hard disk (overwrites the current file) if executing the code results in actual changes in the layer. Running the code below for a second time doesn't delete any additional features bc selId doesn't change and the layer is NOT saved again. The same applies to the code proposed in the answer below. It seems like the automatic saving isn't triggered by the code itself but maybe rather by some general preference setting according which any changes to a layer are automatically saved to the hard disk? Does sth like that exist?