As all plugin developer, I want to delete all options that begin with the same prefix.


I've developed a plugin that stores data in the options. When the user uninstalls the plugin, the uninstall.php in the plugin executes the following code:

if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {

delete_option( 'myplugin_some_opt_1' );
delete_option( 'myplugin_some_opt_2' );
delete_option( 'myplugin_some_opt_3' );
delete_option( 'myplugin_some_opt_4' );

Since all of the options start with myplugin_, I want to implement a wildcard. Logicall, I assume that it would look something like this:

if ( !defined( 'WP_UNINSTALL_PLUGIN' ) ) {

delete_option( 'myplugin_*' );

Replace "myplugin_" with your prefix:

global $wpdb;

$plugin_options = $wpdb->get_results( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'myplugin_%'" );

foreach( $plugin_options as $option ) {
    delete_option( $option->option_name );

I've found out another alternative by using the wp_load_alloptions() function to get all of the available options, then delete_option() for every option that has the myplugin_ prefix:

foreach ( wp_load_alloptions() as $option => $value ) {
    if ( strpos( $option, 'myplugin_' ) === 0 ) {
        delete_option( $option );
  • 1
    works only for autoloaded options – urbansurfers Nov 3 '19 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.