{"id":614,"date":"2016-06-20T15:01:47","date_gmt":"2016-06-20T09:31:47","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=614"},"modified":"2026-09-07T17:01:48","modified_gmt":"2026-09-07T11:31:48","slug":"deleting-memcache-key-on-cluster","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/deleting-memcache-key-on-cluster\/","title":{"rendered":"Script to delete Memcache key on cluster"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When multiple Memcache servers are used in a cluster, removing the same cache key from every server manually can be time-consuming.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following Python script can be used to delete a single Memcache key or multiple Memcache keys across multiple Memcache hosts. The hosts can be grouped into different Memcache clusters, allowing you to target a specific cluster when running the script.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before using the script, make sure the following requirements are satisfied:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The script assumes that you have multiple Memcache clusters listening on different ports. Categorize the Memcache hosts under the <code>MEMCACHE_CLUSTER<\/code> section.<\/li>\n\n\n\n<li>The machine running the script must have the Python Memcache module installed.<\/li>\n\n\n\n<li>The origin host must be able to connect to the Memcache port, which is <code>11211<\/code> by default, on all Memcache servers.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Configure the Memcache Clusters<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The following script contains the Memcache cluster configuration and functions required to delete cache keys:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/usr\/bin\/python\n########Script to Delete Memcache Key########\n#Author:Dhanasekaran N\n#Email:support@pheonixsolutions.com\n\n#Version:1.0\n#############################################\nimport memcache\nfrom fabric import *\nfrom fabric.api import *\n\n#Mention your Cluster hosts and unique name\n_MEMCACHE_CLUSTER_ = {\n\u201cCluster1\u201d :[\u2018IPAddress1:&lt;port>\u2019,\u2019IPaddress2:&lt;port>\u2019],\n\u201cCluster2\u201d :[\u2018IPaddress3:&lt;port>\u2019,\u2019IPaddress:&lt;port>\u2019],\n}\ndef _run_command(host_name,verify_key):\nmc = memcache.Client([host_name], debug=0)\n#run(\u2018pwd\u2019);\nprint \u201ckey name:\u201d + verify_key;\nchecked_key=mc.get(verify_key);\nprint  checked_key;\nif(checked_key is None):\nprint \u201cThe key %s doesn\u2019t exist \u201d  %(verify_key)\nelse:\nprint \u201cDeleting %s from %s\u201d %(verify_key,host_name)\nmc.delete(verify_key)\n\ndef delete_memcache(delete_key=\u2019none\u2019,hosts_group=\u2019none\u2019,delete_multiple_keys=\u2019none\u2019):\nall_groups=_MEMCACHE_CLUSTER_.keys();\n#print delete_multiple_keys;\nif (delete_key==\u2019none\u2019 and  delete_multiple_keys==\u2019none\u2019):\nprint \u201cNo delete Key or delete_multiple_key Mentioned\u201d;\nprint \u201cEg:fab  delete_memcache:delete_key=\u2019some_key1\u2032,hosts_group=\u2019java_api'\u201d\nelif(hosts_group ==\u2019none\u2019):\nprint \u201cMention the host Group while running fab\u201d\nprint _MEMCACHE_CLUSTER_.keys();\nelse:\nif(delete_multiple_keys!=\u2019none\u2019):\nif(delete_multiple_keys):\ndelete_multiple_keys = delete_multiple_keys.split(\u201c;\u201d)\nfor cache_cluster in all_groups:\nif(cache_cluster == hosts_group):\n#print _MEMCACHE_CLUSTER_[cache_cluster];\nfor cache_cluster_hosts in _MEMCACHE_CLUSTER_[cache_cluster]:\nprint cache_cluster_hosts;\nfor delete_key in delete_multiple_keys:\nexecute(_run_command,hosts=cache_cluster_hosts,host_name=cache_cluster_hosts,verify_key=delete_key);\nelse:\nfor cache_cluster in all_groups:\nif(cache_cluster == hosts_group):\nfor cache_cluster_hosts in _MEMCACHE_CLUSTER_[cache_cluster]:\nprint cache_cluster_hosts;\nexecute(_run_command,hosts=cache_cluster_hosts,host_name=cache_cluster_hosts,verify_key=delete_key);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Delete a Single Memcache Key<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To delete a single key from all hosts belonging to <code>Cluster1<\/code>, use:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fab delete_memcache:delete_key=keyname,hosts_group=Cluster1<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This command deletes the specified key from each Memcache host configured under <code>Cluster1<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Delete Multiple Memcache Keys<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To delete multiple keys from a specific cluster, separate the key names using <code>;<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fab delete_memcache:delete_multiple_keys=keyname1;keyname2,hosts_group=Cluster1<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This executes the deletion for each specified key across the hosts configured in <code>Cluster1<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Verify the Key Before Deletion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The script first checks whether the specified key exists:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">checked_key=mc.get(verify_key);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the key does not exist, the script displays a message indicating that the key is unavailable. If the key exists, it proceeds with the deletion:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mc.delete(verify_key)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This provides a basic verification before removing the cache entry.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Managing cache keys across multiple Memcache servers can be difficult when performed manually. This Python and Fabric-based script simplifies the process by allowing administrators to delete a single key or multiple keys across a selected Memcache cluster.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By grouping Memcache hosts into clusters, the required cache entries can be removed from the appropriate servers using a single command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">1. What is the purpose of this Memcache script?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The script is used to delete one or more Memcache keys across multiple hosts belonging to a configured cluster.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Can I delete multiple Memcache keys at once?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Multiple keys can be supplied using the <code>delete_multiple_keys<\/code> option, with each key separated by a semicolon.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3. What port does Memcache use by default?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Memcache commonly uses port <code>11211<\/code>. The script requires the origin server to have network connectivity to the configured Memcache hosts and ports.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">4. Can I target a specific Memcache cluster?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. The <code>hosts_group<\/code> parameter allows you to specify the cluster, such as <code>Cluster1<\/code> or <code>Cluster2<\/code>, where the keys should be deleted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>PHP Fatal Error: Class &#8216;Memcache&#8217; Not Found<\/strong> \u2013 Learn how to troubleshoot the PHP Memcache extension when the <code>Memcache<\/code> class is unavailable in a PHP application.<br><a href=\"https:\/\/pheonixsolutions.com\/blog\/php-fatal-error-class-memcache-not-found-in\/?utm_source=chatgpt.com\" target=\"_blank\" rel=\"noreferrer noopener\">Read the article<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction When multiple Memcache servers are used in a cluster, removing the same cache key from every server manually can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-614","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-9U","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=614"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/614\/revisions"}],"predecessor-version":[{"id":11468,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/614\/revisions\/11468"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}