How to Hard Delete users in DotNetNuke

Last Updated Tuesday, May 17, 2011 7:27 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

UPDATE: Hard Delete is coming to DotNetNuke 5.6.2  http://www.dotnetnuke.com/Resources/Blogs/tabid/825/EntryId/2994/DotNetNuke-Spotlight-User-Management.aspx

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
Hi, I would like delete all users except admin and host. How can I do it ?
Posted By: Antoine Ranou on Saturday, October 16, 2010 8:34 PM
Do I just run this script as is to hard delete all soft deleted users or am I supposed to insert the actual user ID of a specific soft deleted user I want to hard delete before running the script?
Posted By: Justin Elza on Monday, September 27, 2010 10:23 AM
Chris: How are the ASPNET Registration tables affected by hard vs. soft deleting?
Posted By: Laverne Douglas on Friday, September 17, 2010 7:57 PM
Like Antoine, I have also been looking for a way to delete all the users except the admin and host. I have not found a way to do it. Could your script be modified to do that? or Secondly, could I run an update query to set the IsDeleted field to true for all my users - and then run your script?
Posted By: Mark Buelsing on Friday, November 12, 2010 11:10 AM
Mark, I'm sure you could update the script to do that, you would want to modify the query to look for users based on their usernames, and skip 'admin' and 'host'
Posted By: Chris Hammond on Sunday, November 14, 2010 5:50 PM
This was causing me a lot of trouble while setting up OAUTH, Facebook and Twitter authentication for DNN. I have created a SQL Trigger on the PortalUser table to hard delete the users when the IsDeleted bit is set to true.
Posted By: Colin Styles on Friday, January 28, 2011 12:35 AM
Note that this only deletes a single user from the database. If you have more than one "soft deleted", you'll have to run it once for each one.
Posted By: William Phelps on Wednesday, February 23, 2011 8:19 PM
In my opinion, DNN has to provide both facilities, soft delete and hard delete. Some clients really need hard delete of User and need to add then again
Posted By: Sandeep Patil on Monday, March 14, 2011 11:27 PM
Sandeep, I would appreciate it if you REMOVE my blog post that you blatantly stole and posted on your own website (the link to which I removed from your comment). Not cool
Posted By: Chris Hammond on Monday, March 14, 2011 11:34 PM
thanks for code.. but I am sure dnn might have provided something for hard delete
Posted By: Sagar P on Tuesday, March 15, 2011 3:43 PM
This works fine in DNN 5.6, but you may have to remove the {databaseOwner}{objectQualifier} depending on your setup.
Posted By: Patrik Johansson on Tuesday, March 15, 2011 9:39 AM
Sandeep, or Sagar, you can comment on my blog post all you want, but I'm not going to let the comment link to your site. It is nothing but stolen content from other websites.
Posted By: Chris Hammond on Tuesday, March 15, 2011 4:25 PM
Patrik, the code provided is designed to be executed in the HOST/SQL page, which replaces those tokens for you
Posted By: Chris Hammond on Tuesday, March 15, 2011 4:27 PM
How do you delete profile properties in DNN 6.1 and above. I have inadvertently double created a profile property and it is preventing my users from being able to modify their profiles. I did go through Admin- User Accounts - Manage Profile Properties and deleted there, but it leaves them in the SQL Database, which Active Social picks up from the deleted tables. Thanks!!
Posted By: Paul Coffman on Thursday, January 26, 2012 5:35 AM
Paul, I would suggest posting your question in the Community Exchange on DotNetNuke.com, as it is a bit outside the skip of this blog post.
Posted By: Chris Hammond on Thursday, January 26, 2012 2:04 PM