Editing features in QGIS can be risky if a plugin is not designed correctly. In this article, we explain how to use editing mode, commit or roll back changes, and ask for user confirmation, ensuring data safety while keeping things accessible to beginner developers.
Why managing edits is critical in a QGIS plugin
In QGIS, editing a layer is never a trivial operation.
A plugin can work very quickly… and cause considerable damage if it modifies data without proper safeguards.
A good plugin should therefore:
- Modify as little as possible.
- Always know when it is modifying data.
- Be able to undo changes.
- Ask for confirmation whenever necessary.
Editing mode: an essential safety barrier
As explained in the article about working with selected features, a vector layer in QGIS must be in editing mode before it can be modified.
Before making any changes:
if not layer.isEditable():
layer.startEditing()
Without this step, any attempt to modify the layer will fail.
⚠️ A plugin must never assume that the layer is already in editing mode.
Editing a feature: what actually happens
Editing a feature in a plugin does not simply mean:
“Write a value and you’re done.”
In reality, the process involves:
- Modifying a copy held in memory.
- Asking QGIS to apply the changes to the layer.
- Deciding whether to commit or cancel the changes.
Example:
feature["statut"] = "validé"
layer.updateFeature(feature)
At this stage, nothing has yet been permanently written to disk.
Committing changes: commitChanges()
To actually save the changes:
layer.commitChanges()
This is the stage at which:
- The data is written.
- The changes are committed to the data source.
A plugin should never commit changes without a clear reason.
Rolling back changes: rollBack()
If something goes wrong (an error, a business rule violation, or user cancellation):
layer.rollBack()
This cancels:
- All changes made since the last
startEditing()call.
This is equivalent to a global Ctrl+Z for the current editing session.
Typical case: business rule validation failure
For example:
- A calculated value is inconsistent.
- A business rule is not respected.
- A mandatory field is empty.
Recommended approach:
layer.rollBack()
return
It is better to make no changes than to make incorrect ones.
Asking the user for confirmation
A well-designed plugin explains what it is about to do before doing it.
For example:
“You are about to modify 12 features. Do you want to continue?”
This confirmation is particularly important when:
- Several features are affected.
- The modification cannot easily be undone.
- The operation is performed automatically.
Even a simple dialog box can prevent serious problems.
Never force changes silently
Things to avoid:
- Switching to editing mode without informing the user.
- Modifying data and then committing automatically.
- Hiding errors.
A plugin should never take its users by surprise.
Recommended strategy
- Check the layer and the selection.
- Check the business rules.
- Ask the user for confirmation.
- Enter editing mode.
- Apply the changes.
- Commit the changes (
commit) or roll them back (rollback).
It may take a little longer, but it is much safer.
Key takeaways
startEditing()→ enables editing.updateFeature()→ applies changes within the editing session.commitChanges()→ commits changes to the data source.rollBack()→ cancels all changes made during the current editing session.
Plugin safety comes from good logic, not from the amount of code.
🔗 Links to the other articles in the series
🔹 Understanding the context
To put this article into context, you can read:
- Introduction: Why Develop Your Own QGIS Plugins?
- QGIS Plugin Structure: Understanding the Essential Files
🔹 Getting started
🔹 Before Modifying Data
The following articles cover the essential concepts to understand before modifying data:
- Applying Business Logic to Values Entered in a QGIS Plugin
- Working Only with Selected Features in a QGIS Plugin
🔹 Making your plugin more reliable
- Detecting and handling errors in a QGIS plugin (Publication scheduled for December 2th)
- Informing and communicating with the user (Publication scheduled for December 8th)
- Preparing and carrying out your first tests (Publication scheduled for December 15th)
- Debugging and making your code more reliable (Publication scheduled for December 22th)
🔹 Useful Related Articles
These additional articles help avoid common mistakes:



