> For the complete documentation index, see [llms.txt](https://learn.devlabss.my.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.devlabss.my.id/php/bab-8-php-and-mysql-pdo/8.5-menghapus-data-delete.md).

# 8.5 Menghapus Data (Delete)

Operasi `delete` perlu ditangani hati-hati agar data tidak hilang tanpa sengaja.

Bagian ini membahas alur hapus data dan pentingnya konfirmasi tindakan.

### Video pengantar

Tonton video ini untuk mendapat gambaran awal sebelum lanjut ke materi inti.

{% embed url="<https://youtu.be/L-gKceeb61Q?si=V3UwUXBTOCOw5rSe>" %}

### Tujuan belajar

Setelah mempelajari bagian ini, Anda diharapkan bisa:

* memahami fungsi operasi `delete`
* menghapus data berdasarkan id
* memahami kenapa penghapusan harus dilakukan dengan hati-hati

### Apa itu operasi `delete`

`Delete` berarti menghapus data dari database.

Operasi ini biasanya dipakai saat data:

* tidak lagi dibutuhkan
* salah input dan tidak ingin dipakai
* perlu dibersihkan dari sistem

### Contoh query `DELETE`

```php
<?php
$sql = "DELETE FROM siswa WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([1]);
?>
```

### Kenapa harus hati-hati

Data yang sudah dihapus tidak selalu mudah dikembalikan.

Karena itu, penghapusan harus punya target yang jelas dan sebaiknya disertai konfirmasi.

### Contoh alur sederhana

```php
<?php
$id = 1;

$sql = "DELETE FROM siswa WHERE id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id]);

echo "Data berhasil dihapus";
?>
```

### Hal yang perlu diperhatikan

Sebelum menghapus data:

* pastikan id benar
* pastikan pengguna memang ingin menghapus
* hindari query hapus yang terlalu umum

### Kesalahan umum

* lupa `WHERE`
* salah menentukan id
* menghapus data tanpa konfirmasi
* mengira hapus data sama aman dengan edit data

### Latihan singkat

Buat contoh penghapusan data siswa berdasarkan id.

Lalu tambahkan penjelasan pada kode Anda bahwa penghapusan harus dilakukan dengan konfirmasi.

### Poin evaluasi bab 8.5.

Pastikan Anda sudah memahami poin berikut:

* Saya paham fungsi operasi `delete`.
* Saya tahu pentingnya `WHERE` pada query hapus.
* Saya paham penghapusan data harus hati-hati.
* Saya bisa membaca contoh `DELETE` sederhana.

Jika semua sudah jelas, lanjut ke [Praktik: CRUD Data Siswa](/php/bab-8-php-and-mysql-pdo/praktik-crud-data-siswa.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.devlabss.my.id/php/bab-8-php-and-mysql-pdo/8.5-menghapus-data-delete.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
