delete_option()WP 1.2.0

Removes option by name. Prevents removal of protected WordPress options.

Return

true|false. True if the option was deleted, false otherwise.

Usage

delete_option( $option );
$option(string) (required)
Name of the option to delete. Expected to not be SQL-escaped.

Examples

0

#1 Basic example

Remove the myoption option

delete_option( 'myoption' );
0

#2 Let's delete several options at once and make sure they are removed

Here we will try to remove the following options: is_installed, my_plugin_version, my_option.

$deleted = delete_my_options( 'is_installed', 'my_plugin_version', 'my_option' );

if( $deleted ){
	echo 'The settings have been deleted!';
}
else {
	echo 'Deleting settings caused an error. The settings could not be removed!';
}

function delete_my_options() {

	$args = func_get_args();
	$num = count( $args );

	if( $num === 1 ){
		return ( delete_option( $args[0] ) ? true : false );
	}

	if( $num > 1 ){

		foreach( $args as $option ){
			if( ! delete_option( $option ) ){
				return false;
			}
		}

		return true;
	}

	return false;
}
0

#3 Delete all settings

On Plugin deactivation if want to delete all settings.

$to_delete = array( 
	'plugin_status', 
	'export_status', 
	'notifications', 
	'label_settings',
	// etc
); 

// Clear up our settings
foreach ( $to_delete as $name ) {
	delete_option( $name );
}

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 1.2.0 Introduced.

Code of delete_option() WP 5.9.3

function delete_option( $option ) {
	global $wpdb;

	if ( is_scalar( $option ) ) {
		$option = trim( $option );
	}

	if ( empty( $option ) ) {
		return false;
	}

	wp_protect_special_option( $option );

	// Get the ID, if no ID then return.
	$row = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option ) );
	if ( is_null( $row ) ) {
		return false;
	}

	/**
	 * Fires immediately before an option is deleted.
	 *
	 * @since 2.9.0
	 *
	 * @param string $option Name of the option to delete.
	 */
	do_action( 'delete_option', $option );

	$result = $wpdb->delete( $wpdb->options, array( 'option_name' => $option ) );

	if ( ! wp_installing() ) {
		if ( 'yes' === $row->autoload ) {
			$alloptions = wp_load_alloptions( true );
			if ( is_array( $alloptions ) && isset( $alloptions[ $option ] ) ) {
				unset( $alloptions[ $option ] );
				wp_cache_set( 'alloptions', $alloptions, 'options' );
			}
		} else {
			wp_cache_delete( $option, 'options' );
		}
	}

	if ( $result ) {

		/**
		 * Fires after a specific option has been deleted.
		 *
		 * The dynamic portion of the hook name, `$option`, refers to the option name.
		 *
		 * @since 3.0.0
		 *
		 * @param string $option Name of the deleted option.
		 */
		do_action( "delete_option_{$option}", $option );

		/**
		 * Fires after an option has been deleted.
		 *
		 * @since 2.9.0
		 *
		 * @param string $option Name of the deleted option.
		 */
		do_action( 'deleted_option', $option );

		return true;
	}

	return false;
}