How would one go about deleting all option names in a WordPress database beginning with a specific prefix?
I would assume we need to specify a prefix, get all options that begin with that prefix, and then delete each option found.
Here is a sample of the prefix and WP functions for getting and deleting options in the database.
<?php
$prefix = 'cpt_';
$getOpt = get_option($prefix);
foreach($getOpt as $toDelete){
$deleteOpt = delete_option($prefix);
if(!$deleteOpt){
echo 'Failure.';
}
if($deleteOpt){
echo 'Success.';
}
}
?>
Resources:
You could run this query:
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'cpt_%'" );
or put it in a function like so:
function delete_options_prefixed( $prefix ) {
global $wpdb;
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '{$prefix}%'" );
}
delete_options_prefixed( 'cpt_' );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With