Suppose a user of my site has had the right (the correct role with sufficient permissions) to create content of a certain type.

If the right (role) is taken away from them, how could I make the users content automatically be deleted? (the deletion doesn't have to be instant, eg. once a day is fine) i.e the content will be deleted from the moment they no longer have permissions to edit it or create such content.

  • Hmm, don't know a way for that but I must say as An SEO services provider - It might be problematic to lose content without making redirects to proper pages, especially much content; Was important for me to share it here... – user16289 Jan 6 '16 at 8:23

This code gives you the basic idea:

$nodes = node_load_multiple(array(), array('type' => 'my_type'));// load node by certain type
foreach ($nodes as $node){
    $uid= $node->uid;//get the node author id
    $nid = $node->nid;// get the node id
    $author = user_load($uid);//load the author of the node
    if (!in_array('your_role', $author->roles)) { //the author has not your_role role

if you copy this code in the theme_preprocess_html of your template.php, for every page it checks the nodes and its author roles and delete the node id its author has not the role.

If you need to run this code once a user role is updated, you can use Rules module :

enter image description here

I am not able to run this code right now, but it should work correctly. You can use devel module to correctly get the $node properties ($nid and $uid).

| improve this answer | |
  • The code you've shown could cause problems on sites with large amounts of content or low PHP memory limits. (And remember that content increases with time.) I'd really advise against putting it on every page as well. If you're only running it when an account is updated, it would be more efficient to use an EntityFieldQuery to find the NIDs of any content by that particular author. – Andy Jan 6 '16 at 12:02
  • @Andy he must use this code once an user is updated. The more efficient solution is, once an user is updated check if it has a certain role, if yes do nothing otherwise remove his content of a certain content type. This is much more efficient but the solution in the answer will not cause any problem. Because MySQL is very fast in reading but slow in writing, so the major work which is writing will be executed on a limited number of times that is why the above code is safe – M a m a D Jan 6 '16 at 15:20
  • The problem I was getting at is that you're loading all nodes of a particular content type into memory at the same time. This is generally a bad idea. If you really need to load an unknown number of nodes, then you can use the Batch API. In this case though you don't need to load the nodes at all, you just need to get the relevant NIDs and then delete them. Your code as it stands could cause heinous memory usage, and could also bring a site down just because users are creating content (at some point it can cause the memory limit to be blown). – Andy Jan 6 '16 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.