wpdb::delete()
Deletes rows from the table based on the condition specified in the $where parameter.
Enables protection against SQL injections, which means that uncleaned data can be passed, for example: $_GET['foo']...
Method of the class: wpdb{}
No Hooks.
Returns
Int|false. The number of deleted rows or 0 if nothing was deleted. false is returned on query error.
Usage
global $wpdb; $wpdb->delete( $table, $where, $where_format );
- $table(string) (required)
- The name of the table.
- $where(array) (required)
- An array of conditions that will be used to select rows for deletion in the format
[ 'column name' => 'value' ]. Multiple conditions will be combined usingAND. If the value is set toNULL, the query will perform a comparisonIS NULL, and the corresponding format will be ignored. - $where_format(array/string)
An array of data formats that will be associated with the specified values in the $where parameter. If a string is provided, it (the format) will be associated with all data. When a format is specified, WordPress converts the provided data to the specified format before creating the query. Possible formats:
%s- string%d- integer%f- float
If not specified, the format
stringwill be used for all values of $data, unless otherwise specified in the wpdb::$field_types.
Default: null
Examples
#1 Example of deleting data from the database
// Delete row with field ID=1 from table table $wpdb->delete( 'table', [ 'ID' => 1 ] ); // Let's specify the format of the value $where $wpdb->delete( 'table', [ 'ID'=>'1' ], [ '%d' ] ); // 1 will be treated as (int) (%d).
#2 Multiple "where" and "type"
$where = [ 'UID' => 248, 'File' => "C:\file.txt" ]; $where_format = [ '%d', '%s' ]; $wpdb->delete( $table, $where, $where_format );
Notes
- See: wpdb::prepare()
- See: wpdb::$field_types
- See: wp_set_wpdb_vars()
Changelog
| Since 3.4.0 | Introduced. |