delete_option()WP 1.2.0

Deletes settings (post from the wp_options table in the DB).

This function is created for the safe deletion of posts (settings) from the Database.

Returns

bool. true if the setting was found and deleted and false otherwise.

Usage

delete_option( $name );
$name(string) (required)
The name of the option to be deleted.

Examples

#1 Basic example

Remove the myoption option

delete_option( 'myoption' );

#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;
}

#3 Delete all settings

On Plugin deactivation if want to delete all settings.

$to_delete = [
	'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.

delete_option() code WP 7.1

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 ( in_array( $row->autoload, wp_autoload_values_to_autoload(), true ) ) {
			$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' );
		}

		$notoptions = wp_cache_get( 'notoptions', 'options' );

		if ( ! is_array( $notoptions ) ) {
			$notoptions = array();
		}
		$notoptions[ $option ] = true;

		wp_cache_set( 'notoptions', $notoptions, '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;
}