9951 explained code solutions for 126 technologies


predisHow do I delete a key in PHP using Redis?


To delete a key in PHP using Redis, you can use the DEL command. This command takes a single or multiple keys as arguments and deletes them from the Redis database.

Example

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$redis->del('key1');
echo "key1 deleted successfully";
?>

Output example

key1 deleted successfully

The code above:

  • $redis = new Redis();: Creates a new instance of the Redis client.
  • $redis->connect('127.0.0.1', 6379);: Connects to the Redis server.
  • $redis->del('key1');: Deletes the key key1 from the Redis database.
  • echo "key1 deleted successfully";: Prints a message indicating the key was deleted successfully.

Helpful links

Edit this code on GitHub