What would be a PHP command to erase all posts from category X from the last month?

I wish to run a cron job that would permanently erase all the posts belonging to some category from the past X days (say, week).
This is probably very basic, but I would appreciate some pointers.
Thanks.

Solutions Collecting From Web of "What would be a PHP command to erase all posts from category X from the last month?"

The first step is setting up the cron job.

The second part requires querying the database for a specific post type where the entry is older than 1 week. We can do this with get_posts() and specifying the category argument and the date_query argument.

//* If the scheduled event got removed from the cron schedule, re-add it
if( ! wp_next_scheduled( 'wpse_263953_remove_old_entries' ) ) {
  wp_schedule_event( time(), 'daily', 'wpse_263953_remove_old_entries' );
}

//* Add action to hook fired by cron event
add_action( 'wpse_263953_remove_old_entries', 'wpse_263953_remove_old_entries' );
function wpse_263953_remove_old_entries() {
  //* Get all posts older than 7 days...
  $posts = get_posts( [
    'numberposts' => -1,
    //* Use `cat` to query the category ID
    //* 'cat' => 'wpse_263953_category_id',
    //* Use `category_name` to query the category slug
    'category_name' => 'wpse_263953_category',
    'date_query' => [
      'after' => date( "Y-m-d H:i:s", strtotime( '-7 days', current_time( 'timestamp' ) ) ),
      //* For posts older than a month, use '-1 months' in strtotime()
    ],
  ]);
  //* ...and delete them
  foreach( $posts as $post ) {
    wp_delete_post( $post->ID );
  }
}

Following Query will give you list of Post ID older than 30 days for given Category ID.

SELECT  
    p.ID as post_id, 
    term.term_id as category_id
FROM wp_posts as p 
LEFT JOIN wp_term_relationships as tr ON tr.object_id = p.ID
LEFT JOIN wp_terms as term ON tr.term_taxonomy_id = term.term_id
WHERE term.term_id = "CATEGORY ID HERE" 
    AND DATEDIFF(NOW(), p.post_date) > 30

Once you get the list you can decide weather you want to delete post or not.

To Delete the post use following function, the second parameter ( TRUE ) will delete the post permanently. If you want to keep the post in trash pass ( FALSE )

wp_delete_post( 'YOUR_POST_ID_HERE', TRUE);

Dont forget to change The Table prefix if its not ‘wp_’