How to delete object with Id in Realm or Primary Key?

2.1k Views Asked by At

I have below object :

obj1 = [{ id = 1, name = "abc"}, {id=2, name="pqr"}, {id=3, name="xyz"}]

I need to delete an object with id=2 where id is Primary Key too. Below method using to delete the object

 const collection = RealmDB.realm
      .objects("StudentName")
      .filtered(`id= $0`, '65');
    RealmDB.realm.write(() => {
      RealmDB.realm.delete(collection);
    });

But it is not working with id object can anyone please suggest better way to do this ?

But still that object is there so may I know what is wrong here.

2

There are 2 best solutions below

3
On

Hello resolved an issue by following query with no error

const collection = RealmDB.realm
          .objects('StudentName')
          .filtered('id= $0', `65`);
        RealmDB.realm.write(() => {
          RealmDB.realm.delete(collection);

Thank you

0
On
const id = 1;
realm.write(()=>{
   realm.delete(realm.objectForPrimaryKey('Baby',id));
}) 

Try this.