Jump to Section
Understanding the Basics: Deactivating vs. Deleting User in Umbraco#
Umbraco's back-office panel, by default, doesn’t allow the outright removal of a user.
Instead, it provides a feature to deactivate them.
But what if you need more?
Why Soft Delete Isn’t Always Enough#
For compliance reasons, such as GDPR, merely deactivating users might not suffice.
You might need to ensure that a user's data is eradicated from the system entirely.
Here's where hard deletion comes into play.
When working with relational databases, it's essential to understand the interconnections between tables, especially when it comes to deletions.
The user table in a system like Umbraco isn't isolated.
Instead, it's linked to multiple other tables through foreign key relationships or other references.
Deleting a record from the user table without addressing these related references can lead to inconsistencies, orphaned records, and errors in the system.
Why You Can't Simply Remove Data from the Umbraco User Table#
-
Referential Integrity: Most modern databases enforce referential integrity, which ensures that relationships between tables remain consistent. If a user ID in the [umbracoUser] table is referenced in another table, attempting to delete that user without addressing the reference will lead to a violation of this integrity, and the database will prevent the deletion.
-
Orphaned Records: Even if referential integrity isn't enforced, deleting a user without addressing related data would leave orphaned records in other tables. These are records that no longer have a meaningful reference to the main table, which can result in anomalies and inaccuracies in the data.
-
Functional Issues: From an application perspective, orphaned records or broken references can lead to unexpected behavior, errors, or crashes when the system tries to access or operate on data that no longer exists or is inconsistent.
When you're considering deleting a user from the [umbracoUser] table in Umbraco, you're not just affecting that single table.
The user data and its associated references sprawl across various tables in the database:
-
[dbo].[umbracoContentVersion]: This table tracks the different versions of content. If a user has made changes or created content, their userID might be associated with various content versions. Deleting the user without addressing this could lead to orphaned content versions with no associated user.
-
[dbo].[umbracoNode]: The Node table represents the basic content units in Umbraco. If a user has created or is associated with certain nodes, their userID could be found here. Deleting the user would leave nodes with missing creator or modifier references.
-
[dbo].[umbracoLog]: This is a logging table where various user activities might be recorded. If the user has performed actions that generated log entries, their userID would be referenced in this table.
-
[dbo].[umbracoUser2UserGroup]: This table manages the relationship between users and user groups. Deleting a user would mean there are entries in this table that refer to a non-existent user.
-
[dbo].[umbracoUserLogin]: Tracks the login activities of users. If the user has ever logged in, there would be entries with their userID in this table.
-
[dbo].[umbracoUser2NodeNotify]: Manages notifications related to nodes for users. Deleting a user would orphan notifications meant for them.
-
[dbo].[umbracoUserStartNode]: Determines the starting node in the content tree for a user when they log in. Deleting a user would mean that there could be starting nodes without a linked user.
Given these interconnected references, you have three main strategies:
-
Remove User with all associated data: You'd have to go through each of the above tables and remove entries associated with the user's userID. This ensures there are no orphaned references but also results in data loss.
-
Detach User data by setting userId to null: Go through each table and replace the user's userID with NULL. This method retains the records but removes the direct association to the user. However, some operations or reports might not work as expected with NULL userIDs.
-
Reassign to a System User with userId Set to -1: Instead of deleting or nullifying, you'd reassign the user's references in each table to a system user (typically with a userID of -1). This means data remains intact but is no longer linked to the original user.
It's crucial to handle such operations with caution, ensuring that data integrity is maintained and the Umbraco system continues to function seamlessly.
Hard Deleting an Umbraco User with SQL
📌 Ensure you back up your database for safety. The script provided is compatible with Umbraco 8.6.0.
Incorporating transactions, especially when altering multiple data points as in the provided script, is a best practice in database management.
It provides an essential safety net against unintended alterations and potential data corruption.
Here's an overview of the script:
-
Transaction Initiation: The BEGIN TRAN command initiates a transaction, signaling the start of a series of database operations that should be treated as a single unit.
-
User Identification: The script determines the ID of a user with the email 'hello@piotrbach.com' from the [dbo].[umbracoUser] table. This ID is stored in the variable @userId.
-
User Reference Updates: The subsequent UPDATE statements are operations to nullify references to the specified user ID in various tables. This action detaches the data from the identified user without deleting the actual records.
-
Data Deletion: The DELETE statements target records specifically associated with the identified user across multiple tables, purging the user's associations.
-
User Record Removal: The script culminates with the removal of the user's primary record from the [dbo].[umbracoUser] table.
-
Transaction Conclusion: The ROLLBACK TRAN command reverts all the changes made during the transaction. There's an alternative command commented out (-- COMMIT TRAN), which, when activated, would save the changes to the database permanently.
Addressing User-Related Content After Deletion#
After you delete a user, the content they authored will no longer display their name as the author.
However content is still available and it's possible to restore previous versions of the content if needed.
For a visual guide, see the attached screenshot:
Bonus Tip: Hiding Disabled Users in Umbraco Backoffice#
Umbraco provides an option to hide disabled users.
You need to take the following steps:
-
- Go to the umbracoSettings.config file located at ~/config/umbracoSettings.config.
- Find the tag hideDisabledUsersInBackoffice.
- Set its value to true.
The "hideDisabledUsersInBackoffice" feature in Umbraco is a thoughtful and practical addition for several reasons:
-
Improved User Management: It simplifies user management by decluttering the back office view. Admins can focus on active users without sifting through disabled accounts.
-
Enhanced Security: Hiding disabled users can improve security. It ensures that any account that shouldn't have access is also not visible, reducing potential confusion or misuse.
-
Better Privacy: It helps in maintaining privacy. Disabled users' details are kept out of sight, which can be crucial for former employees or users who have left the organization.
-
Flexibility: It provides flexibility. Instead of permanently deleting a user, you can disable and hide them. This allows for the easy reactivation of the user if necessary.
-
Easy to Implement: The feature is simple to enable with just a change in the configuration file. It doesn't require extensive technical knowledge or complex steps.
In summary, this feature is a cool addition to Umbraco because it enhances user interface cleanliness, security, privacy, and overall user management efficiency with minimal effort.
Final words#
In the dynamic world of Umbraco management, understanding the nuances between deactivating and deleting users is crucial, especially when ensuring GDPR compliance.
By following this guide, you can confidently and securely manage user data.
If you have any queries or need assistance, contact us for expert guidance.
Also, don't forget to browse our blog for more insightful articles on Umbraco and other CMS platforms.
Want the code? Check out this article's source code on our GitHub.