perform batch deletion or fuzzy deletion of keys in Redis - ApsaraDB for Redis (2024)

The DEL key command of Redis is used to delete a single key. To batch delete keys, you can combine the cat and xargs commands of Linux and the DEL command of Redis. If you want to delete keys that have the same prefix or suffix by using fuzzy match logic, we recommend that you use the SCAN and DEL commands of Redis.

Batch delete specified keys

Delete multiple specified keys

Before you run the command, write the list of keys that you want to delete to a file.

Run the cat command to read each line from the file as Redis keys. Then, use redis-cli to connect to the Redis server and run the DEL command to delete the keys. Command syntax:

cat <file> | xargs redis-cli -h <host> -a <password> DEL

Note

The xargs command can divide a long list of arguments into small chunks and then pass them as arguments to other commands. This prevents the "Argument list too long" error in Linux. You can use the command alone or together with pipe operators, redirection operators, or other commands.

Parameters:

Example:

cat key.txt | xargs redis-cli -h r-bp127cu5tb*****.redis.rds.aliyuncs.com -a Test**** DEL

Delete keys by using fuzzy match logic

Method 1 (recommended): Use the SCAN and DEL commands

Iteratively scan and delete the keys that meet specific conditions. Sample code:

import redisimport sysdef main(argv): if len(argv) < 4: print("Usage: python scan_and_del.py host port password match") sys.exit(-1) host = argv[1] port = argv[2] password = argv[3] match = argv[4] print("host: %s, port: %s, password: %s, match: %s\n" % (host, port, password, match)) redis_cli = redis.StrictRedis(host=host, port=port, password=password) cursor = 0 rnd = 1 while rnd == 1 or cursor != 0: result = redis_cli.scan(cursor=cursor, match=match, count=1000) ret_cursor = result[0] ret_keys = result[1] num_keys = len(ret_keys) print("del %d keys in round %d, cursor: %d" % (num_keys, rnd, ret_cursor)) if 0 != num_keys: ret_del = redis_cli.delete(*ret_keys) print("ret_del: %d, ret_keys: %s" % (ret_del, ret_keys)) cursor = ret_cursor rnd += 1 print("")if __name__ == '__main__': main(sys.argv)

Call the scan_and_del.py script that contains the preceding sample code and specify the connection information of the ApsaraDB for Redis instance and the key matching pattern to implement fuzzy deletion.

python scan_and_del.py <host> <port> <password> "<key>" 

Parameters:

  • host: the endpoint that is used to connect to the ApsaraDB for Redis instance. For more information, see View endpoints.

  • port: the port number of the ApsaraDB for Redis instance.

  • password: the password that is used to connect to the ApsaraDB for Redis instance. For more information, see Logon methods.

  • key: the keys that meet specific conditions in a database. For example, "test*" matches keys that start with test, such as test1, test2, and test3.

    Wildcard match description:

    • w?rld: matches five-character strings that start with w and end with rl, such as world, warld, and wxrld.

    • w*rld: matches any string that starts with w and ends with rl, such as wrld and woooorld.

    • w[ae]rld: matches warld and werld, but does not match world.

    • w[^e]rld: matches world and warld, but does not match werld.

    • w[a-b]rld: matches warld and wbrld.

Method 2: Use the KEYS and DEL commands

Warning

  • The KEYS command may cause high CPU utilization. We recommend that you use this command during off-peak hours.

  • If you use the KEYS command in a large database, the database performance is compromised. We recommend that you use this command when you process small amounts of data.

Run the KEYS command to perform wildcard matching for all keys that meet specific conditions, and then run the DEL command to delete the keys.

redis-cli -h <host> -a <password> KEYS "<key>" | xargs redis-cli -h <host> -a <password> DEL

Parameters:

  • file: the file that contains the keys to be deleted. Example: key.txt.

  • password: the password that is used to connect to the ApsaraDB for Redis instance. For more information, see Logon methods.

  • key: the keys that meet specific conditions in a database. For example, "test*" matches keys that start with test, such as test1, test2, and test3.

    Wildcard match description:

    • w?rld: matches five-character strings that start with w and end with rl, such as world, warld, and wxrld.

    • w*rld: matches any string that starts with w and ends with rl, such as wrld and woooorld.

    • w[ae]rld: matches warld and werld, but does not match world.

    • w[^e]rld: matches world and warld, but does not match werld.

    • w[a-b]rld: matches warld and wbrld.

Example:

redis-cli -h r-bp127cu5tb*****.redis.rds.aliyuncs.com -a Test**** KEYS "keys*" | xargs redis-cli -h r-bp127cu5tb*****.redis.rds.aliyuncs.com -a Test**** DEL

Verify whether the specified keys are deleted

redis-cli -h <host> -a <password> KEYS "<key>"
perform batch deletion or fuzzy deletion of keys in Redis - ApsaraDB for Redis (2024)
Top Articles
Basics of What a Credit Bureau Is and Does, Plus Major Ones
How To Check Flash Memory Of Cisco Switch? - GeeksforGeeks
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
Things To Do In Atlanta Tomorrow Night
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5848

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.