Redis

Redis Delete Key

Redis, unlike relational databases, does not support languages such as SQL. Instead, it uses simple commands and command options to manipulate databases and the information stored in them.

This tutorial will go over the basics of working with Redis commands and learn how to delete keys in a Redis data store.

Inserting Redis Keys

Redis is a key-value data store. A key refers to a unique string of characters identifying a specific value stored in the data store. Redis uses string type for keys.

Before we learn how to delete keys, let us first discuss creating keys and inserting data in Redis.

Open the terminal and open the redis command-line interface using the command:

redis-cli

If your Redis cluster is hosted on the local machine, you should automatically drop into the Redis CLI tool as

127.0.0.1:6379>

The above example prompt shows the IP address and the port on which the Redis cluster is running.

If you are running Redis on a different host, you can specify the IP address using the -h option and the respective port using the -p flag.

Example connection is as shown below:

redis-cli -h 192.168.0.211 -p 6379

To create a key in Redis, we use the SET command. It takes two arguments separated by a space. The first argument represents the key, and the second represents the value associated with the key.

For example, to create a key “username” that stores the value “John Doe” we can do:

SET username "John Doe"

If the command executes successfully, you see a friendly OK messaging.

127.0.0.1:6379> SET username "John Doe"
OK

Unlike SQL language, you do not have to end your commands in a semi-colon. Doing so will result in an invalid arguments error:

127.0.0.1:6379> SET username "John Doe";
Invalid argument(s)

Fetching Keys and Values.

If you know the name of the key, you can retrieve the stored value by using the GET command. It takes the name of the key as the argument.

For example, to the value stored in the key “username”:

GET username
"John Doe"

As seen in the example above, the command should return the corresponding value associated with the specified key.

Deleting Keys

Deleting a key in Redis is as simple as creating one. We use the DEL command followed by the name of the key we wish to remove.

For example, to remove the key “username”:

DEL username
(integer) 1

Redis will drop the key and its associated data if the specified key exists in the data store.

You can also use the DEL command to remove multiple keys in a single instance.

For example, suppose we have a Redis database containing the US states. The database contains the code for each state as the key and the value as the State name.

Hence, LA -> “Los Angeles”

We can delete several keys using the DEL command as:

192.168.100.78:6379> del LA AZ CO
(integer) 3

In the example command, we remove keys: LA, AZ, and CO, which correspond to values: “Los Angeles,” “Arizona,” and “Colorado,” respectively.

Closing

In this guide, you learned the basics of Redis commands, including creating new keys and values, getting the values associated with a specific key, and removing keys and values from the database.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list