Title: Delete old posts programmatically
Last modified: August 20, 2016

---

# Delete old posts programmatically

 *  [scrollpost](https://wordpress.org/support/users/scrollpost/)
 * (@scrollpost)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/delete-old-posts-programmatically/)
 * I want to delete old posts programmatically.
    I found smatterings of code but
   usually more complicated than I require.
 * I put this together to delete posts older than 120 days.
 * Will it work?
 * >  <?php
   >  require_once ‘wp-load.php’;
   > global $wpdb;
   > $expireds = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE DAY(TIMEDIFF(
   > NOW(), post_date_gmt)) >=120”);
   > foreach ($expireds as $expired) {
   >  wp_delete_post($post->id, true); } ?>

The topic ‘Delete old posts programmatically’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [scrollpost](https://wordpress.org/support/users/scrollpost/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/delete-old-posts-programmatically/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
