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
Budget-Friendly Hotel Chains Everything You Need to Know about Hotel Cancellation Policies
30-Day Money Cleanse
WALB Locker Room Report Week 5 2024
News - Rachel Stevens at RachelStevens.com
Paris 2024: Kellie Harrington has 'no more mountains' as double Olympic champion retires
Practical Magic 123Movies
Localfedex.com
Puretalkusa.com/Amac
How To Get Free Credits On Smartjailmail
The Pope's Exorcist Showtimes Near Cinemark Hollywood Movies 20
Weapons Storehouse Nyt Crossword
Lesson 3 Homework Practice Measures Of Variation Answer Key
Bme Flowchart Psu
Rapv Springfield Ma
7440 Dean Martin Dr Suite 204 Directions
Sand Castle Parents Guide
Wizard Build Season 28
Hocus Pocus Showtimes Near Amstar Cinema 16 - Macon
How to Create Your Very Own Crossword Puzzle
Zalog Forum
V-Pay: Sicherheit, Kosten und Alternativen - BankingGeek
Evil Dead Rise - Everything You Need To Know
Is The Yankees Game Postponed Tonight
PCM.daily - Discussion Forum: Classique du Grand Duché
F45 Training O'fallon Il Photos
Naya Padkar Gujarati News Paper
Craigslist Dubuque Iowa Pets
Idle Skilling Ascension
Publix Near 12401 International Drive
Cona Physical Therapy
Yale College Confidential 2027
Meijer Deli Trays Brochure
Shiny Flower Belinda
O'reilly's In Monroe Georgia
The Goonies Showtimes Near Marcus Rosemount Cinema
My Reading Manga Gay
Craigslist Auburn Al
Angela Muto Ronnie's Mom
Att U Verse Outage Map
Best Restaurant In Glendale Az
Sabrina Scharf Net Worth
“To be able to” and “to be allowed to” – Ersatzformen von “can” | sofatutor.com
Barstool Sports Gif
Mychart University Of Iowa Hospital
Avatar: The Way Of Water Showtimes Near Jasper 8 Theatres
Candise Yang Acupuncture
The Blackening Showtimes Near Ncg Cinema - Grand Blanc Trillium
Aloha Kitchen Florence Menu
Missed Connections Dayton Ohio
Jigidi Jigsaw Puzzles Free
What Are Routing Numbers And How Do You Find Them? | MoneyTransfers.com
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.