1. 1 items are tagged with 5.4
  2. 1 items are tagged with Admin
  3. 5 items are tagged with Administration
  4. 1 items are tagged with Award
  5. 1 items are tagged with Bay Area
  6. 2 items are tagged with California
  7. 2 items are tagged with Community
  8. 1 items are tagged with Conference
  9. 1 items are tagged with Contest
  10. 1 items are tagged with Control Panel
  11. 1 items are tagged with Corporation
  12. 2 items are tagged with Daily Tips
  13. 1 items are tagged with Delete
  14. 3 items are tagged with Development
  15. 6 items are tagged with DNN
  16. 1 items are tagged with DNN Corp
  17. 1 items are tagged with DNNUG
  18. 8 items are tagged with DotNetNuke
  19. 1 items are tagged with DotNetNuke Connections
  20. 1 items are tagged with DotNetNuke Friendly URLs
  21. 1 items are tagged with DotNetNuke Tips
  22. 1 items are tagged with Email
  23. 1 items are tagged with Engage Software
  24. 2 items are tagged with Europe
  25. 5 items are tagged with Events
  26. 1 items are tagged with Facebook
  27. 1 items are tagged with Forum
  28. 1 items are tagged with Friendly URLs
  29. 1 items are tagged with Groups
  30. 1 items are tagged with Hard Delete
  31. 1 items are tagged with Host
  32. 1 items are tagged with INETA
  33. 1 items are tagged with Las Vegas
  34. 1 items are tagged with Mandalay Bay
  35. 1 items are tagged with Module
  36. 1 items are tagged with modules
  37. 1 items are tagged with NavigateURL
  38. 1 items are tagged with News
  39. 1 items are tagged with OpenForce
  40. 1 items are tagged with Paris
  41. 1 items are tagged with Personal
  42. 2 items are tagged with Presentations
  43. 1 items are tagged with RadEditor
  44. 2 items are tagged with release
  45. 1 items are tagged with San Francisco
  46. 2 items are tagged with Skinning
  47. 2 items are tagged with Travel
  48. 3 items are tagged with upgrade
  49. 2 items are tagged with upgrades
  50. 2 items are tagged with Users

How to Hard Delete users in DotNetNuke

Last Updated Wednesday, February 24, 2010 4:41 PM


By: Chris Hammond

One of the changes to DNN over the past year or so was the way that users were handled when they were deleted. Originally when a user was deleted from DotNetNuke they were hard deleted and completely removed from the database. This would cause problems if you had multiple portals (websites) in your instance of DNN and had users who were shared across those portals.

To correct for the issue, users are now soft deleted from a portal, there is a UserPortals table that contains an isdeleted flag whenever someone is removed.

This is fine and dandy in most cases, but what if you really want to delete the user completely from the database? Well then you need to come up with some SQL to do so as there are a number of tables that need to get hit. While perusing through the forums today I came across a post in which a user was having problems with deleted users and the requirement for unique email addresses in the web.config. If the some of the users with unique email addresses were soft deleted the user who wasn’t deleted couldn’t login to the site. To correct for this I wrote some SQL to attempt to hard delete the users.

A word of Warning: I’ve done only a limited amount of testing on this, you should definitely backup your database before running ANY custom SQL. Also be sure to test this out in a test environment first to make sure you don’t have any negative impacts. This doesn’t handle custom modules and their data with user references, so you might have to delete a few other items before you can delete the data referenced below.

delete {databaseOwner}{objectQualifier}userroles where userid=(select top 1 userid from {databaseOwner}{objectQualifier}userportals where isdeleted=1 and userid not in (select userid from {databaseOwner}{objectQualifier}userportals where isdeleted=0))


delete {databaseOwner}{objectQualifier}userprofile where userid=(select top 1 userid from {databaseOwner}{objectQualifier}userportals where isdeleted=1 and userid not in (select userid from {databaseOwner}{objectQualifier}userportals where isdeleted=0))


delete aspnet_membership where userid=( select userid from aspnet_users where username=(select username from {databaseOwner}{objectQualifier}users where userid=(select top 1 userid from {databaseOwner}{objectQualifier}userportals where isdeleted=1 and userid not in (select userid from {databaseOwner}{objectQualifier}userportals where isdeleted=0))))


delete aspnet_users where username=(select username from {databaseOwner}{objectQualifier}users where userid=(select top 1 userid from {databaseOwner}{objectQualifier}userportals where isdeleted=1 and userid not in (select userid from {databaseOwner}{objectQualifier}userportals where isdeleted=0)))
delete {databaseOwner}{objectQualifier}userportals where userid=(select top 1 userid from {databaseOwner}{objectQualifier}userportals where isdeleted=1 and userid not in (select userid from {databaseOwner}{objectQualifier}userportals where isdeleted=0))


delete {databaseOwner}{objectQualifier}users where userid not in (select userid from {databaseOwner}{objectQualifier}userportals) and issuperuser=0

Rate this:
Recent Comments
I have had the same problem. Nice tip!
Posted By: Brad Bamford on Monday, February 22, 2010 7:20 PM
Nice article. On the flip side, we're running a large membership driven site where it's not un-common for users to be soft-deleted but then the Admin needs to re-instate them. There's no built in facility in DNN to "restore" a user name to a portal. Any tips? Cheers Ian
Posted By: Ian Sampson on Wednesday, February 24, 2010 5:45 PM

I have to agree with Ian here. What's the purpose of soft delete when there is no restoring capability (at least, the easy for simple DNN users)? I also think that whenever a user is deleted Admin should get the choice to delete that user "softly" or "hardly." :)

Posted By: Frozen DotNetNuke on Thursday, February 25, 2010 12:01 PM
This doesn't work unfortunately as the IsDeleted flag isn't set anymore in the newer versions of DNN. This whole area is very broken.
Posted By: Neil Burnett on Friday, March 19, 2010 9:16 AM
Neil, I just tested this on a DNN 5.3.0 instance and it works just as it did when I posted the sample code. What version are you running on?
Posted By: Chris Hammond on Friday, March 19, 2010 11:17 AM
Wierd i just noticed i have the same problem with deleted users showing in Superuser and tried the above but to no avail. 5.2.3 is my current dnn version.
Posted By: Craig Mitchell on Sunday, March 28, 2010 8:49 AM
The above code WILL NOT delete superuser accounts, so if you need to remove those you'll have to use different SQL
Posted By: Chris Hammond on Sunday, March 28, 2010 1:49 PM
If I'm not wrong, using these procedures all the user profiles and roles wont be deleted, so a "large" amount of useless rows will be left in the db. To completely remove all the related data, I think that is necessary to do similar "delete" commands on tables "Profile", "UserProfile" and "UserRoles"..
Posted By: Alberto Biasiutti on Tuesday, March 30, 2010 9:05 AM
Alberto, unfortunately I think you missed reading some of the code that I have in my blog post. UserRoles and UserProfile data does get removed in my example. You may be correct about Profile though, that would be a fairly easy one to grab as well though with the above SQL as an example
Posted By: Chris Hammond on Tuesday, March 30, 2010 11:49 AM
Yes, you're right! :) Sorry, I gave just a short look to the code.. my fault! However, thank you for the tip!
Posted By: Alberto Biasiutti on Tuesday, March 30, 2010 12:47 PM
Executing a softdelete in dnn will not only set isdeleted in userportals but delete the corresponding userroles. If there are any roles left to the userid, they will belong to another subportal!? It might be an idea to do a count on userroles und to only proceed the harddelete if there isnt any role left! I think there is no need to delete the userroles or userprofiles cause they will be automatically deleted if you delete in users.
Posted By: chris grage on Wednesday, March 31, 2010 1:53 PM
Thanks for this, Chris. Saved me a couple of hours easily. Jason
Posted By: Jason Kergosien on Wednesday, March 31, 2010 1:59 PM
Thanks for sharing, it really help me. Anyway, you forgot to include delete from "UserAuthentication".
Posted By: erwin yulianto on Thursday, April 15, 2010 12:09 PM
Erwin, good catch. I will try to get that added in and update the blog later this week.
Posted By: Chris Hammond on Thursday, April 15, 2010 12:52 PM
Hello, I've tried running this SQL and all I get are a bunch of Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '}'. I know less than a newbie, but enough to be dangerous. What am I doing wrong?
Posted By: luke schaedle on Friday, May 14, 2010 9:12 AM
Luke, this SQL is designed to be executed in the DNN Host/SQL window
Posted By: Chris Hammond on Friday, May 14, 2010 9:40 AM
... and finally - your script works perfectly:-) Thanks!
Posted By: Neil Burnett on Sunday, May 23, 2010 1:09 PM
Hi Chris - I just got round to testing if the IsDeleted flag is actually changed when deleting a user in DNN 5.4.2. Is isn't. That means anything that depends on the IsDeleted flag showing True when a user has been deleted won't work. As I said in March, DNN has become severely broken when deleting users. It neither hard nor soft deletes.
Posted By: Neil Burnett on Sunday, May 23, 2010 1:00 PM
... I see that all a delete does is set the UserPortals.IsDeleted flag to true, leaving the other user tables untouched. I guess it is a multiple portals thing the author is trying to cover.
Posted By: Neil Burnett on Sunday, May 23, 2010 1:05 PM
This has caused problems on our Intranet. There should be an option when deleting users so that you can completely remove the user, or just soft delete them from that portal. I have been running the script from time to time to purge deleted users.
Posted By: Mike Pratt on Thursday, July 29, 2010 3:30 PM
I just tested this script on DNN v05.05.00 and it worked well. Definitely needed when you are doing testing. Thanks
Posted By: Rob Ralston on Wednesday, August 25, 2010 7:11 PM