0

Currently I'm doing a mysql query via the command line using Drush and I want to parse the query output and grab the Node ID #, then put that ID # into a drush php-cli 'node_delete(X)' command. How can I do this? Currently I have the command:

drush sql-query 'select * from node where type="feed_item" AND status=0 limit 1 \G'

then

drush php-eval 'node_delete(X)'

I think I'm suppose to use the command xargs?

From there how can I make this as a loop and delete everything that get's outputted? Yes I know I can just do it via the site, but i want to know how to do it via the command line using only Drush. Eventually I just want to make an interactive bash script where I can just put in the node ID range numbers and it will just delete it at once.

1

Based on the query that you've provided, you're just getting one unpublished feed_item, then deleting it. This can be done in one line, without an intermediate bash step.

drush ev "node_delete(
  db_select('node')
    ->fields('node', array('nid'))
    ->condition('status', '0', '=')
    ->condition('type', 'feed_item', '=')
    ->range(0, 1)
    ->execute()
    ->fetchField()
);"

If you wanted to delete multiple nodes at once - or all nodes returned with your conditions, then use https://api.drupal.org/api/drupal/modules%21node%21node.module/function/node_delete_multiple/7 , remove the range and use fetchCol() to return an array of NIDs.

drush ev "node_delete_multiple(
  db_select('node')
    ->fields('node', array('nid'))
    ->condition('status', '0', '=')
    ->condition('type', 'feed_item', '=')
    ->execute()
    ->fetchCol()
);"

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.