Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Created September 1, 2026 10:44
Show Gist options
  • Select an option

  • Save dwanjuki/c55d0850ac088c17d2aa0c2c0c0c34a7 to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/c55d0850ac088c17d2aa0c2c0c0c34a7 to your computer and use it in GitHub Desktop.
Delete all sandbox orders and sandbox subscriptions.
<?php
/**
* Delete all sandbox orders and sandbox subscriptions.
* WARNING: This permanently deletes data. Back up your database first!
*
* Does not cancel anything at the gateway.
* If necessary, delete Sandbox subscriptions via gateway dashboard.
*
* Run it ONCE: visit /wp-admin/?pmpro_delete_sandbox_data=1 as an admin,
* then remove this snippet.
*
* You can add this snippet to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_delete_sandbox_data() {
if ( empty( $_REQUEST['pmpro_delete_sandbox_data'] ) || ! current_user_can( 'manage_options' ) ) {
return;
}
global $wpdb;
// Orders.
$order_ids = $wpdb->get_col( "SELECT id FROM $wpdb->pmpro_membership_orders WHERE gateway_environment = 'sandbox'" );
if ( ! empty( $order_ids ) ) {
$ids = implode( ',', array_map( 'intval', $order_ids ) );
$wpdb->query( "DELETE FROM $wpdb->pmpro_membership_ordermeta WHERE pmpro_membership_order_id IN ($ids)" );
$wpdb->query( "DELETE FROM $wpdb->pmpro_membership_orders WHERE id IN ($ids)" );
}
// Subscriptions.
$sub_ids = $wpdb->get_col( "SELECT id FROM $wpdb->pmpro_subscriptions WHERE gateway_environment = 'sandbox'" );
if ( ! empty( $sub_ids ) ) {
$ids = implode( ',', array_map( 'intval', $sub_ids ) );
$wpdb->query( "DELETE FROM $wpdb->pmpro_subscriptionmeta WHERE pmpro_subscription_id IN ($ids)" );
$wpdb->query( "DELETE FROM $wpdb->pmpro_subscriptions WHERE id IN ($ids)" );
}
wp_die(
sprintf(
'Deleted %d sandbox orders and %d sandbox subscriptions. Remove this snippet now.',
count( $order_ids ),
count( $sub_ids )
)
);
}
add_action( 'admin_init', 'my_pmpro_delete_sandbox_data' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment