<p>From the <a href="http://buildwithcraft.com/docs/plugins/hooks-and-events#events" rel="nofollow">documentation</a> on events, they follow this pattern:</p>

<pre><code>craft()-&gt;on('component.eventName', function(Event $event) {
    // ...
});
</code></pre>

<p>So in your example, it'd be something like this and you'd want to add it to your plugin's <code>init()</code> method in the main plugin class.</p>

<pre><code>public function init()
{
    parent::init();

    craft()-&gt;on('users.onDeleteUser', function(Event $event) {
        $user = $event-&gt;params['user'];
        $userToTransferContentTo = $event-&gt;params['transferContentTo']
    });
}
</code></pre>

<p>The full list of available events and the parameters they have can be found <a href="http://buildwithcraft.com/docs/plugins/events-reference" rel="nofollow">here</a>.</p>

CC BY-SA 3.0
2015-11-06T19:24:26.107Z
Thanks, it doesn't seem to work though. I've edited the question to show how my code looks at the moment.
H2ONOCK
CC BY-SA 3.0
2015-11-06T19:26:08.483Z
Updated my answer.
Brad Bell
CC BY-SA 3.0
2015-11-06T19:38:47.920Z
Thanks Brad, that really helps. I feel like I'm close now. I can't seem to find how to get the user's id from that $user var which I believe to be of UserModel type? I've updated my question with the latest code.
H2ONOCK
CC BY-SA 3.0
2015-11-06T19:39:47.720Z
You're correct and that should do it!
Brad Bell
CC BY-SA 3.0
2015-11-07T11:25:37.500Z
It doesn't :( I'm pretty sure it's not firing. The same code to cancel a membership is used elsewhere so I know that works. I'm not sure `$userId = $user->id;` is correct because I can't see an `id` mentioned in the UserModel documentation.
H2ONOCK
CC BY-SA 3.0
2015-11-08T21:17:27.747Z
I WORKED IT OUT! I'd stupidly misinterpreted what 'main plugin class' meant. Thanks for all of your help on this :)
H2ONOCK
CC BY-SA 3.0
2015-11-06T18:43:54.360Z
2015-11-06T19:26:00.333Z
2015-11-06T19:26:00.333Z
Brad Bell
<p>I'm currently building a plugin that uses a table that contains rows with an 'active' field set to 1.</p>

<p>If an admin deletes a user from the system, I want to change this value to 0. </p>

<p>I found an 'onUserDelete' event documented but can't work out where or how to use it.</p>

<p>Any help would be greatly appreciated!</p>

<p>EDIT UPDATED:</p>

<p>I now have this code in the init() function of my plugin controller but deleting a user does not seem to fire the code:</p>

<pre><code>    parent::init();

    craft()-&gt;on('users.onDeleteUser', function(Event $event) {
        $user = $event-&gt;params['user'];

        $userId = $user-&gt;id; // ???????

        $craft-&gt;memberships_membership-&gt;cancelMembershipById($userId);
    });
</code></pre>

<p>Any ideas?</p>

<plugin-development>
How can I tap into onUserDelete?
# How can I tap into onUserDelete?

(Asked by: username_0 on 2015-11-06)

I'm currently building a plugin that uses a table that contains rows with an 'active' field set to 1.
If an admin deletes a user from the system, I want to change this value to 0. 
I found an 'onUserDelete' event documented but can't work out where or how to use it.
Any help would be greatly appreciated!
EDIT UPDATED:
I now have this code in the init() function of my plugin controller but deleting a user does not seem to fire the code:
    parent::init();

    craft()->on('users.onDeleteUser', function(Event $event) {
        $user = $event->params['user'];

        $userId = $user->id; // ???????

        $craft->memberships_membership->cancelMembershipById($userId);
    });

Any ideas?

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2015-11-06. Score: 1 | Accepted answer

From the documentation on events, they follow this pattern:
craft()->on('component.eventName', function(Event $event) {
    // ...
});

So in your example, it'd be something like this and you'd want to add it to your plugin's init() method in the main plugin class.
public function init()
{
    parent::init();

    craft()->on('users.onDeleteUser', function(Event $event) {
        $user = $event->params['user'];
        $userToTransferContentTo = $event->params['transferContentTo']
    });
}

The full list of available events and the parameters they have can be found here.

> **Comments:**
> **username_0**: Thanks, it doesn't seem to work though. I've edited the question to show how my code looks at the moment.  
> **username_1**: Updated my answer.  
> **username_0**: Thanks Brad, that really helps. I feel like I'm close now. I can't seem to find how to get the user's id from that $user var which I believe to be of UserModel type? I've updated my question with the latest code.  
> **username_1**: You're correct and that should do it!  
> **username_0**: It doesn't :( I'm pretty sure it's not firing. The same code to cancel a membership is used elsewhere so I know that works. I'm not sure `$userId = $user->id;` is correct because I can't see an `id` mentioned in the UserModel documentation.  
> **username_0**: I WORKED IT OUT! I'd stupidly misinterpreted what 'main plugin class' meant. Thanks for all of your help on this :)  

---

