get_sites() for sites registered before given date

Here, this should permanently delete all sites older than 30 days (except the one with ID 1). I was not able to make it work with date_query.

If you need to call get_sites() for these sites for some other reason, just instead of deleting them, add their IDs in an array, and then call get_sites() only for sites in this array. To get more sites than 100 you need to change the number argument, as I have done below.

$delete_on_timestamp = time() - 3600*24*30;

$args = array (
    'site__not_in' => array ( 1 ),
    'fields' => 'ids',
    'number' => 999999,
);

$site_ids = get_sites( $args );

foreach( $site_ids as $site_id ) {
    $object_site_details = get_blog_details( $site_id );
    $site_register_timestamp = strtotime( $object_site_details->registered );
    if ( $site_register_timestamp <= $delete_on_timestamp ) {
        wpmu_delete_blog( $site_id, true );
    }
}

Keep in mind that the registered time is stored in the timezone of the main site, unlike the last updated time, which uses GMT time. I have reported this problem here and it should be fixed.

Also keep in mind that if you have a lot of sites, this code will timeout. To fix this you can do this several times in smaller chunks, by using the offset and the number argument.

error code: 523