ayush
ayush

Reputation: 14568

node_delete not working

I am trying to delete some CCK nodes in Drupal using a standalone PHP script while logged in as anonymous user

if(empty($total_deals_for_this_pl)){
$node_nid = $single_result['nid'];
global $user;
$original_user = $user;
$user = user_load(1);
print $node_nid."<br>";
node_delete($node_nid);
$user = $original_user;
}

I am able to retrieve all the nid's successfully but the nodes are not getting deleted. I am loading Drupal as follows

chdir('C:\wamp\www\mysite\platform'); //my drupal resides here
require_once './includes/bootstrap.inc';
include_once './includes/common.inc';

Upvotes: 0

Views: 1085

Answers (2)

jpstrikesback
jpstrikesback

Reputation: 2308

Node_delete() has an access check for delete permissions inside it.

Test again with anonymous users given permission to delete nodes.

Also try adding

drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

If that doesn't work you could try up to the session phase:

drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);

and finally the full dealio:

drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

Upvotes: 3

CurtainDog
CurtainDog

Reputation: 3205

Three options:

  1. Generally, I would recommend using VBO for this kind of thing. Its a more robust solution than a custom script. It's pretty easy to set up and once you've used it you'll probably think of a dozen other ways to use it.
  2. Failing that, make your own module and stick your custom script inside a proper hook. Your custom script on its own might not be playing along with what other modules are expecting.
  3. If you still want to have your own separate script I suspect it's the bootstrap code that's failing. Check out drupal_bootstrap for the options available to you.

Upvotes: 2

Related Questions