Delete a User with Rules

Events happening in the community are now at Drupal community events on www.drupal.org.
targoo's picture

Hi folk,

I working with Drupal since a year but I am quiet new with Rules.

I want to delete a user if they do not confirm their registration after a week. It is almost done with all the process but I still need a action to delete the user. This action seems not to be in the core. Is there any reason why ? I guest I need to create a new custom action ?

Thanks for your advice,

Cheers,

Comments

And/or sending them a request to revalidate

amir simantov's picture
  • using toboggan.module.

Thanks but I have some issues

targoo's picture

Thanks but I have some issues to make Login Toboggan and Domain work together so I decided not to use Toboggan.

Rules scheduler!

itangalo's picture

There is an action for deleting user accounts in rules. You could use it like this:

0) Activate Rules Scheduler (included in the Rules project). Also make sure that Token is installed and running.
1) Create a rule set for deleting a user account (with a user as argument)
2) Create a rule triggering on account created, that schedules the rule set in one week from the current time ('+7 days'). Give the scheduled rule a unique ID, say delete uid [user:uid]
3) Create a rule triggering on user login. Have it delete any scheduled rule with ID delete uid [user:uid]

Done!

//Johan Falk, NodeOne, Sweden

Thanks

targoo's picture

Thanks, that was also my idea.

I am running under :
Rules 6.x-1.2
Token 6.x-1.12

and I get no 'User Delete' in my configurable or not configurable list of actions which is normal when you check the code of Rules :

function user_rules_action_info() {
return array(
'rules_action_user_addrole' => array(
'label' => t('Add user role'),
'arguments' => array(
'user' => array('type' => 'user', 'label' => t('User whos roles should be changed')),
),
'module' => 'User',
),
'rules_action_user_removerole' => array(
'label' => t('Remove user role'),
'arguments' => array(
'user' => array('type' => 'user', 'label' => t('User whos roles should be changed')),
),
'module' => 'User',
),
'rules_action_load_user' => array(
'label' => t('Load a user account'),
'new variables' => array(
'user_loaded' => array('type' => 'user', 'label' => t('Loaded user')),
),
'help' => t('Enter an id or a name of the user to load.'),
'module' => 'User',
'eval input' => array('userid', 'username'),
),
'rules_action_user_create' => array(
'label' => t('Create a user'),
'arguments' => array(
'username' => array('type' => 'string', 'label' => t('User name')),
'email' => array('type' => 'string', 'label' => t("User's E-mail")),
),
'new variables' => array(
'user_added' => array('type' => 'user', 'label' => t('New user')),
'user_added_pw' => array('type' => 'string', 'label' => t("New user's password")),
),
'module' => 'User',
),
);
}

So I am gonna add a new action for the user.

Which version are you using to get a delete action for the user ?

Cheers,

Check 'X actions not configurable'

itangalo's picture

On a Rules 1.1 install I have lying around I find the 'Delete User' option under the section 'User', in the list of available actions.

If you can't see this action, it is probably because you don't have any user object available. That's pretty strange, since you almost always have the logged in user available.

I suggest the following:

  1. Look at the 'X actions not configurable', which you find under the available actions, to see that the 'Delete user' action is included in your Rules version. Otherwise, download and install again.
  2. If you haven't any user object available, add an action to load the relevant user object.

Good luck!
//Johan Falk, NodeOne, Sweden

No Delete User at all

targoo's picture

I am afraid I do have all the actions on the User (add role, delete role, ...) but not the delete user. Even if I check the 'X actions not configurable'

I will try to download Rules 1.1 and compare it with 1.2.

Weird...

itangalo's picture

This is really weird. I just tried Rules 1.2, and I have the 'Delete User' action available.
Could it possibly be that you're using an account that doesn't have permission to delete users? (I haven't thought about Rules respecting permissions in this way, but perhaps it does.)

//Johan Falk, NodeOne, Sweden

Provided by Views Bulk Operations

justachris's picture

I know this is a old conversation, but I recently came across this issue as well. The "Delete User" action is provided by Views Bulk Operations, specifically delete.action.inc in the 6.x branch.

Unfortunately...

bigpepper's picture

I have to second targoo, I don't see "Delete User". Surprising really, thought it would be a core option

I used Custom PHP to remove a user with rules

bigpepper's picture
  • Set up a 'Rule Set' with Arguments: "User"
  • Add a rule in this rule set, with a condition of the select role of user you want to delete (!!!IMPORTANT!!! Otherwise you may delete yourself!)
  • For the "Do" part, choose custom php, and put in: user_delete(null, $user->uid);

  • Create a Triggered rule, I chose on user logout...

  • For the "Do" part, select the 'Rule Set' we just created.

Hope this helps, was driving me mental. :)

delete user after x days complete

Pooortrait's picture

THANK YOU!!!
that was the rescue of my quiet simple schedule rule : )))

i need to learn more about the php scripts...
i will keep the custom php modi activated : )
for the next time...

Welcome

bigpepper's picture

Glad you fixed things. =)

Can you please delete my account....!!!!

sagarngurnani009's picture

Can you please delete my account....!!!!

I don't want to continue with the services you provide.

Thanks,
Sagar Gurnani
sagarngurnani009@yahoo.com

Drupal 7 and Rules

rachelf's picture

Just adding a note for how I did this with Drupal 7 and Rules, in case anyone searching comes across this thread - the trick was to use delete entity in rules rather than look under the user section for cancel user account.

I used the Views Rules module to generate a list of users that I wanted to delete according to whatever criteria. I then selected the User ID to be passed to Rules (in the view, set rules settings - row variables).

I then set up a rule that was triggered on the event 'Cron maintenance tasks are performed'.

In the rules actions, I then used a Views loop, that loops over the results returned by my view of the list of users to delete, and used the action fetch entity by id (using the User ID I'd passed from my rules view) and then the action delete entity.

Drupal 7 Rule solution

stephen ollman's picture

Here is a rule export I created for D7 that deletes all new user accounts that have not logged in since having the account created nor has a role assigned.

It has a 7 day buffer before deleting the accounts.

{ "rules_delete_never_logged_in_accounts_after_1_week_" : {
    "LABEL" : "Delete Never Logged in accounts after 1 week.",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "php", "rules" ],
    "ON" : { "cron" : [] },
    "IF" : [
      { "php_eval" : { "code" : "$expire_time = REQUEST_TIME - 604800; \r\n$found=0;\r\ndrupal_set_message(\u0027Checking for new user accounts that have not logged on within the last 7 days....\u0027);\r\n\r\n $query = db_select(\u0027users\u0027, \u0027u\u0027);\r\n $query-\u003Efields(\u0027u\u0027, array(\u0027uid\u0027))\r\n       -\u003Econdition(\u0027u.uid\u0027, 0,\u0027\u003E\u0027)  \r\n      -\u003Econdition(\u0027u.access\u0027, 0,\u0027=\u0027)\r\n      -\u003Econdition(\u0027u.login\u0027, 0,\u0027=\u0027)\r\n      -\u003Econdition(\u0027u.created\u0027, $expire_time,\u0027\u003C\u0027)\r\n      -\u003Ewhere(\u0027u.uid NOT IN(select uid from {users_roles})\u0027);  \r\n$result = $query-\u003Eexecute();\r\n\r\nwhile($record = $result-\u003EfetchAssoc()) {\r\n   drupal_set_message(\u0027Found user account ID:\u0027.$record[\u0022uid\u0022]);\r\n   user_delete($record[\u0022uid\u0022]);\r\n   ++$found;\r\n}\r\nif($found \u003E= 1) {\r\n  drupal_set_message(\u0027Deleted \u0027.$found.\u0027 accounts\u0027);\r\n}  else {\r\n  drupal_set_message(\u0027No new accounts deleted\u0027);\r\n}\r\n\r\nreturn true;" } }
    ],
    "DO" : []
  }
}

Just copy the code and import it into Rules.

Alter this Rule for specific user role

jberg1's picture

Hey Stephen, Thanks for this.
One question how could I alter this code to only delete users with a specific role assigned.

Thanks for any help.

Nevermind.

jberg1's picture

I figured it out.
Just changed the line.
->where('u.uid NOT IN(select uid from {users_roles})');

To
->where('u.uid IN(select uid from {users_roles} WHERE rid=5)');

rid= Role ID

Thanks. Let me know if there is a better way.

Drupal version 7.60 with

joshuautley's picture

Drupal version 7.60 with Rules 7.x-2.1

Within your Rule under Actions choose "Delete Entity"

Parameter: Entity: [profile2:user]

Here is our rule for deleting users with the same first and last name after they create an account...

{ "rules_name_match_notification" : {
    "LABEL" : "Name Match Notification",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "profile2" ],
    "ON" : { "profile2_update" : [] },
    "IF" : [
      { "data_is" : {
          "data" : [ "profile2:user:profile-member:field-first-name" ],
          "value" : "[profile2:field_last_name]"
        }
      }
    ],
    "DO" : [
      { "mail" : {
          "to" : "info@force11.org",
          "subject" : "Name Match Notification",
          "message" : "Name: [profile2:field_first_name] = [profile2:field_last_name]",
          "language" : [ "" ]
        }
      },
      { "entity_delete" : { "data" : [ "profile2:user" ] } }
    ]
  }
}

Also, this module looked like an option as well...

https://www.drupal.org/project/user_prune

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: