{"id":26238,"date":"2022-07-06T11:07:39","date_gmt":"2022-07-06T01:07:39","guid":{"rendered":"https:\/\/database.guide\/?p=26238"},"modified":"2022-07-08T12:02:11","modified_gmt":"2022-07-08T02:02:11","slug":"redis-rename-command-explained","status":"publish","type":"post","link":"https:\/\/database.guide\/redis-rename-command-explained\/","title":{"rendered":"Redis RENAME Command Explained"},"content":{"rendered":"\n<p>In Redis, the <code>RENAME<\/code> command renames a key. It allows us to give a key a new name.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p>The syntax goes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RENAME key newkey<\/code><\/pre>\n\n\n\n<p>Where <code><em>key<\/em><\/code> is the old key name and <code><em>newkey<\/em><\/code> is the new one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RENAME colour color<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>In this case I renamed a key called <code>colour<\/code> to <code>color<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s check its value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET color<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Red\"<\/pre>\n\n\n\n<p>If we use the <code>KEYS<\/code> command to check whether the original key exists, we can see that it doesn&#8217;t:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>KEYS colour<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(empty array)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When the New Key Already Exists<\/h2>\n\n\n\n<p>If the new key already exists, it is overwritten. More specifically, Redis performs an implicit <code>DEL<\/code> operation (which could impact performance if the key&#8217;s value is large).<\/p>\n\n\n\n<p>Here&#8217;s an example of renaming a key to key that already exists:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RENAME named_color color<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">OK<\/pre>\n\n\n\n<p>Here, the new key is the same one I used in the previous example, but the old key is a different one. <\/p>\n\n\n\n<p>Now when I get the value of the <code>color<\/code> key, it contains a different value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GET color<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"Green\"<\/pre>\n\n\n\n<p>In the previous example, this key had a value of <code>Red<\/code>, but now it has a value of <code>Green<\/code>. That&#8217;s because the <code>named_color<\/code> key had a value of <code>Green<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Old Key Doesn&#8217;t Exist<\/h2>\n\n\n\n<p>If we try to rename a key that doesn&#8217;t exist, an error is returned:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RENAME nonexistentkey newkey<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(error) ERR no such key<\/pre>\n\n\n\n<p>Also see the Redis <code><a href=\"https:\/\/database.guide\/redis-renamenx-command-explained\/\" data-type=\"post\" data-id=\"26248\">RENAMENX<\/a><\/code> command, which only renames the key if the new one doesn&#8217;t already exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Redis, the RENAME command renames a key. It allows us to give a key a new name.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[153],"tags":[154,20],"class_list":["post-26238","post","type-post","status-publish","format-standard","hentry","category-redis","tag-commands","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=26238"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26238\/revisions"}],"predecessor-version":[{"id":26391,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/26238\/revisions\/26391"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=26238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=26238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=26238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}