If you have drush access to your server, you can use the drush php-script
command (or drush scr
for short) to run a script on top of a full Drupal bootstrap. This allows you to run any Drupal functions or access information you'd customarily have available in a Drupal module on your server, without the overhead of writing an entire custom module for a one-time operation.
Using comment_delete_multiple() to delete comments will ensure that additional field data is removed and statistics get updated during the deletion process. This script should get the job done. Back up your database before running!
<?php
// Change this to the nid you want to clear comments from
$nid = 12345;
// Queries the database for all comment IDs associated with the nid, max 1000
$cids = db_select('comment', 'c')
->fields('c', array('cid'))
->condition('c.nid', $nid)
->range(0, 1000)
->execute()
->fetchCol();
if (!count($cids)) {
drush_print("No more comments found. Aborting.");
// Abort with an error code (so a bash script can key off that)
exit(1);
}
// Delete the comments with comment IDs we got from the DB
comment_delete_multiple($cids);
drush_print(count($cids) . " comments deleted");
Upload this script to the web server and you can execute it from your local machine (with a properly configured drush alias), or the server itself, using drush @your-server-alias scr
. Since you'll need to run it multiple times (it only deletes 1000 comments at a time to avoid timeouts or out-of-memory errors), you'll probably want to use a cron job, or a bash script with a while loop, to keep running it until all the comments are deleted. Here's a bash script that should do it:
#!/bin/bash
while drush @your-server-alias scr /path/to/script/on/server/so.php; do
sleep 1
done
The bash script can be run on the server itself, or your local machine if you have drush configured to talk to your server from your local. If you get 500 errors or other failures while the script is running, you may need to decrease the second value on the ->range(0, 1000)
line. (You will see one error at the end when there are no comments left, by the design of the PHP script, so don't worry about that)