# How do I delete rows based on multiple fields using entities?

**URL:** <https://discourse.cakephp.org/t/how-do-i-delete-rows-based-on-multiple-fields-using-entities/9121>\
**Category:** Uncategorized\
**Created:** [March 12, 2021, 2:19pm UTC](https://discourse.cakephp.org/t/how-do-i-delete-rows-based-on-multiple-fields-using-entities/9121 "2021-03-12T14:19:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiagog](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/thiagog/32/2138_2.png) [@thiagog](https://discourse.cakephp.org/u/thiagog)\
**Post date:** [March 12, 2021, 2:19pm UTC](https://discourse.cakephp.org/t/how-do-i-delete-rows-based-on-multiple-fields-using-entities/9121/1 "2021-03-12T14:19:07Z")

</div>

I would like to know how I delete rows based on both the id of the record and also another field, let’s say, email address; in this case, the user’s email.

```
$contactsTable= TableRegistry::getTableLocator()->get('Contacts');
$contactEntity=$contactsTable->get($Id);
$contactsTable->delete($contactEntity);

```

This successfully deletes the row by its id, but the problem is that I also need to delete the row based on the user’s email.

My `contacts` table:

```
id 
name
email 
phone
address 
user_email

```

In this case, the `user_email` is the email of the user who saved the contact and I would like to only delete that contact when both id and the user email match.

How do I do it using entities? I took a look at the documentation but I couldn’t find anythiing regarding that.

> [Deleting Data - 4.x](https://book.cakephp.org/4/en/orm/deleting-data.html)

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 12, 2021, 2:48pm UTC](https://discourse.cakephp.org/t/how-do-i-delete-rows-based-on-multiple-fields-using-entities/9121/2 "2021-03-12T14:48:52Z")

</div>

What you’re looking for is not really a different way to delete, but a different way to `find` the entity that you want to delete. You’ll need to use `$contactsTable->find()->where([....])->first()`. This will return `null` if there’s no match, or the first record otherwise (presumably, you can have only one match; if there can be multiples, then you’ll need to decide how to handle that).

---

<div class="post-metadata">

**Author:** ![thiagog](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/thiagog/32/2138_2.png) [@thiagog](https://discourse.cakephp.org/u/thiagog)\
**Post date:** [March 12, 2021, 3:07pm UTC](https://discourse.cakephp.org/t/how-do-i-delete-rows-based-on-multiple-fields-using-entities/9121/3 "2021-03-12T15:07:39Z")

</div>

Now that makes sense. Haha, thank you.
