Skip to content

Instantly share code, notes, and snippets.

@BronsonQuick
Last active June 19, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save BronsonQuick/5a7d148e08c3dcab6072 to your computer and use it in GitHub Desktop.
Save BronsonQuick/5a7d148e08c3dcab6072 to your computer and use it in GitHub Desktop.
Delete All WooCommerce Subscriptions
<?php
/*
Plugin Name: Delete All Subscriptions
Plugin URI:
Description: Delete all of those bad boys
Author:
Author URI:
Version: 1.0
*/
function mystic_delete_all_subscriptions() {
$subscriptions = WC_Subscriptions::get_subscriptions(
array(
'subscriptions_per_page' => 1000,
)
);
foreach( $subscriptions as $subscription ) {
WC_Subscriptions_Manager::delete_subscription( $subscription['user_id'], $subscription['subscription_id'] );
}
}
add_action( 'admin_footer', 'mystic_delete_all_subscriptions' );
function mystic_force_delete_subscription( $subscription_can_be_changed, $subscription ) {
return true;
}
add_filter( 'woocommerce_subscription_can_be_changed_to_deleted', 'mystic_force_delete_subscription', 10, 2 );
@eversionsystems
Copy link

I placed this code in my functions.php file of my theme but it didn't seem to delete anything. Saw a lot of errors appearing saying the following

Undefined index: user_id in....
Undefined index: subscription_id in...

I am using version 2.1.1 of WooCommerce Subscriptions.

@NickGreen
Copy link

This is an updated version that might work better for you: https://gist.github.com/JPry/ed3f472d537408bc276a128d200842a8

<?php
/**
 * Plugin Name: Delete all subscriptions.
 * Plugin URI: https://gist.github.com/JPry/ed3f472d537408bc276a128d200842a8
 * Description: PERMANENTLY delete all subscriptions.
 * Version: 1.0
 * Author: Jeremy Pry
 * Author URI: http://jeremypry.com/
 * License: GPL2
 */
// Prevent direct access to this file
if ( ! defined( 'ABSPATH' ) ) {
	die( "You can't do anything by accessing this file directly." );
}
add_action( 'plugins_loaded', 'jpry_delete_all_subscriptions' );
/**
 * Delete all available subscriptions.
 *
 * WARNING: There is no going back. There is no undo.
 *
 * @author Jeremy Pry
 */
function jpry_delete_all_subscriptions() {
	$subscriptions = wcs_get_subscriptions( array(
		'subscriptions_per_page' => 1000,
	) );
	/** @var WC_Subscription $subscription */
	foreach ( $subscriptions as $subscription ) {
		// If you want to allow for all of the proper hooks to be triggered, uncomment the next line.
		//$subscription->update_status( 'cancelled' );
		// This is the point of no return. After ths line the subscription is gone.
		wp_delete_post( $subscription->get_id(), true );
	}
	// Explicit cleanup.
	unset( $subscriptions );
}

@rangler2
Copy link

For anyone who might come across this and not know - in the UI subscription list page, under 'screen options' you can increase the number per page to any size you like. If your server is fast enough you can then delete hundreds at a time via the UI.

@sagarspatil
Copy link

@NickGreen - Thanks. That worked for me.

@davidperezgar
Copy link

Does it sends an email to the user?

@BronsonQuick
Copy link
Author

@davidperezgar I wrote this a long time ago so I can't recall if it does. The code base for WC and Subscriptions has probably change a lot based on some of the other comments from others.

@NickGreen
Copy link

Simply deleting a post using wp_delete_post doesn't send anything. And even if you uncomment the subscription status change as shown in my version of the script, by default, there is no customer email that is sent when a subscription gets cancelled. You would have to add a line there that sends a custom email to the customer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment