Home > other >  how to delete all redis data based on entity or schema
how to delete all redis data based on entity or schema

Time:11-06

I want to delete all redis data that belongs to specific schema or entity regardless of key/data of each entry. Here is how my client look like.

//set up
const { Client, Entity, Schema } = require('redis-om');

class userEntity extends Entity {}

userSchema = new Schema(
  userEntity,
  {
    id: { type: 'string' },
    info: { type: 'string' },
  },
  {
    dataStructure: 'JSON',
  },
);

userRepository = this.redisClient.fetchRepository(userSchema);
await userRepository.createIndex();

//save data
let userData = this.userRepository.createEntity();
userData.id = '123';
userData.info = 'some user';
id = await this.userRepository.save(userData);

await this.userRepository.expire(id, 3600);

So how to delete all records at once? The records that belongs to either userSchema or userEntity?

CodePudding user response:

Unfortunately, RediSearch does not yet have a command that will delete records that match a particular search. And thus neither does Redis OM. The only way to delete found records is to fetch all the matching entityIds and call .remove for them. This is fairly easy however:

const ids = await repo.search().where('foo').eq('bar').return.allIds();
await repo.remove(...ids)

Also, I note that the syntax you are using for Redis OM is from an earlier version. You might want to consider updating to Redis OM 0.4.x as some of the syntax has changed. Details can be found in the README for Redis OM.

  • Related