# If the file is deleted, delete from the ElasticSearch index

**URL:** <https://discuss.elastic.co/t/if-the-file-is-deleted-delete-from-the-elasticsearch-index/325314>\
**Category:** Elasticsearch\
**Created:** [February 11, 2023, 3:26pm UTC](https://discuss.elastic.co/t/if-the-file-is-deleted-delete-from-the-elasticsearch-index/325314 "2023-02-11T15:26:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SplendX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/splendx/32/116560_2.png) [@SplendX](https://discuss.elastic.co/u/SplendX)\
**Post date:** [February 11, 2023, 3:26pm UTC](https://discuss.elastic.co/t/if-the-file-is-deleted-delete-from-the-elasticsearch-index/325314/1 "2023-02-11T15:26:55Z")

</div>

I'm trying to make a piece of code that will be responsible for deleting an indexed file from the elasticsearch index, I pass with the indexed file md5(file name), to the id value. It is necessary to make sure that when deleting a file from a folder, the deletion code by id (md5) is executed. I wrote part of the script, but I understand that it is incorrect because if there is no file, then md5 will not be created, please help)

```auto
<?php

      $path = __DIR__. ("/uploads/Арне.pptx");
      $filename = basename($path);
      $md5 = md5_file($path);
      print($md5); //Передаю переменную md5 в 'id' индексации elasticsearch 

//echo 'MD5-хеш файла ' . $filename . ': ' . md5_file($path);

      if (empty(md5_file($path))) {//Провожу проверку, если md5 не совпадает с именем файла в папке
          $params = [ //Произвожу удаление индекса из ES
            'index' => 'bower',
            'id' => $md5
          ];
            $respose = $client->delete($params);
      } else {
            echo '' . ' есть, файл существует';
      }

      ?>

```

---

<div class="post-metadata">

**Author:** ![SplendX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/splendx/32/116560_2.png) [@SplendX](https://discuss.elastic.co/u/SplendX)\
**Post date:** [February 13, 2023, 11:43am UTC](https://discuss.elastic.co/t/if-the-file-is-deleted-delete-from-the-elasticsearch-index/325314/2 "2023-02-13T11:43:52Z")

</div>

You can close the topic, the issue is resolved. Solved the problem: Iterating over the array in the uploads folder, and iterating over the elasticsearch array id, then used array\_diff, compared the arrays to exclude files that are not in the uploads folder, but they were in the elasticsearch index

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 13, 2023, 11:44am UTC](https://discuss.elastic.co/t/if-the-file-is-deleted-delete-from-the-elasticsearch-index/325314/3 "2023-03-13T11:44:41Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
