Title: Delete older comments
Last modified: August 31, 2016

---

# Delete older comments

 *  Resolved [Bogdan Khrupa](https://wordpress.org/support/users/lizardfreddi/)
 * (@lizardfreddi)
 * [10 years ago](https://wordpress.org/support/topic/delete-older-comments/)
 * Hello,
 * I support very old blog. In the database over 50,000 comments 🙂
    I need to delete
   the comments more than one year older. Delete the comments will need to periodically.
 * Your plugin I really liked! but it removes all comments.
 * I decided to add the ability to delete the comments older than X days.
 * So:
 * I created a tabs to separate functionality.
 * I created a small steps wizard:
    1. Specify older than x days to delete the comments
   2. How many to delete comments in a single pass 3. Get amount of old comments
   4. Confirm deletion 5. Waiting successful deletion
 * All functionality is implemented using `wp_ajax` hook. Used the JavaScript to
   polling server. `setTimeout` run deletion pass in cycle.
    Comments are removed
   via `wp_delete_comment()`.
 * I needed remove over 40,000 older comments, older than 365 days. As well as to
   update the posts information and remove commentsMeta.
 * I could not deal with wordpress SVN repository 🙁
    I loaded code on the GitHub
   [https://github.com/bkhrupa/wordpress-delete-all-comments](https://github.com/bkhrupa/wordpress-delete-all-comments)
 * If you like my modification, please add my addon in your plugin 🙂
    If you have
   a repository on the gitHub – I do pull request.
 * Thank you!
 * [https://wordpress.org/plugins/delete-all-comments/](https://wordpress.org/plugins/delete-all-comments/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Pete](https://wordpress.org/support/users/perthmetro/)
 * (@perthmetro)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/delete-older-comments/#post-8541693)
 * [http://wpquestions.com/Function_to_delete_all_post_comments_older_than_x_days/19600](http://wpquestions.com/Function_to_delete_all_post_comments_older_than_x_days/19600)
 *  [Pete](https://wordpress.org/support/users/perthmetro/)
 * (@perthmetro)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/delete-older-comments/#post-8541696)
 *     ```
       function md_scheduled_delete_comments() {
       $comments = get_comments( array(
       'post_type' => 'my_post_type',
       'date_query' => array(
       'before' => '7 days ago',
       ),
       ) );
   
       if ( $comments ) {
       foreach ( $comments as $comment ) {
       wp_delete_comment( $comment );
       }
       }
       }
       add_action( 'wp_scheduled_delete', 'md_scheduled_delete_comments' );
       ```
   
 * It is triggered by built in WP Cron.
 *  [Pete](https://wordpress.org/support/users/perthmetro/)
 * (@perthmetro)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/delete-older-comments/#post-8541702)
 *     ```
       add_filter( 'template_redirect', 'remove_old_comments' );
       function remove_old_comments() {
       	global $wpdb, $post;
   
       	if ( is_single() && ($post->post_type == 'post') ) {
   
       		$days = 9; // replace with number of days...
       		$date = date("Y-m-d", strtotime("-{$days} days") ) ;
       		$wpdb->query( 
       			$wpdb->prepare( 
       				"
       				 DELETE FROM $wpdb->comments
       				 WHERE comment_post_ID = %d
       				 AND comment_date <= %s
       				",
       				 $post->ID, $date 
       			)
       		);
       	}
       }
       ```
   
    -  This reply was modified 9 years, 4 months ago by [Pete](https://wordpress.org/support/users/perthmetro/).
    -  This reply was modified 9 years, 4 months ago by [Pete](https://wordpress.org/support/users/perthmetro/).
 *  [Pete](https://wordpress.org/support/users/perthmetro/)
 * (@perthmetro)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/delete-older-comments/#post-8541705)
 *     ```
       add_filter( 'comments_template_query_args', 'remove_old_comments' );
       function remove_old_comments($comment_args) {
       	global $wpdb, $post;
   
       	if ($post->post_type == 'post') {
   
       		$date = date("Y-m-d", strtotime("-9 months") ) ;
       		$wpdb->query( 
       			$wpdb->prepare( 
       				"
       				 DELETE FROM $wpdb->comments
       				 WHERE comment_post_ID = %d
       				 AND comment_date <= %s
       				",
       					$post->ID, $date 
       			)
       		);
   
       	}
       	return $comment_args;
       }
       ```
   
 *  Thread Starter [Bogdan Khrupa](https://wordpress.org/support/users/lizardfreddi/)
 * (@lizardfreddi)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/delete-older-comments/#post-8542275)
 * Thanks, i’m test it

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Delete older comments’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/delete-all-comments.svg)
 * [Delete All Comments](https://wordpress.org/plugins/delete-all-comments/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/delete-all-comments/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/delete-all-comments/)
 * [Active Topics](https://wordpress.org/support/plugin/delete-all-comments/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/delete-all-comments/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/delete-all-comments/reviews/)

 * 5 replies
 * 2 participants
 * Last reply from: [Bogdan Khrupa](https://wordpress.org/support/users/lizardfreddi/)
 * Last activity: [9 years, 4 months ago](https://wordpress.org/support/topic/delete-older-comments/#post-8542275)
 * Status: resolved