# Delete a database

Delete a database and everything it holds, or keep its backups for the retention period with a final backup.

Replace _ewdb-0123456789ab_ with your database's id.

**Warning:**
Without a final backup, nothing is kept: the backups go with the database, **including the ones you
took with Back up now**, and a deleted database cannot be restored from.

## Prerequisites

- Permission for `rds:DeleteDBInstance`; to change deletion protection, `rds:ModifyDBInstance`.
- Deletion protection off. While it is on, a delete is refused with `400` "Deletion protection is
  enabled. Disable it before deleting." In the console, **Delete database** stays disabled.
- For a final backup: a database in status `available` with backups on (retention above 0 days).
  Otherwise the request is refused with `409` before anything is deleted: "Backups are off for this
  database, so there is nothing to keep a final backup in." or "A final backup needs the database
  running; it is _status_." A final backup of a database already deleted with one is refused with
  `400`.
- In the console, also `rds:DescribeDBInstances` to open the database's page (without it the page is
  not found).

## Turn deletion protection on or off

**Console**
1. Open the database, then choose **Danger zone**.
2. Turn **Deletion protection** on or off. The change is saved at once.

**CLI**
```sh
dcloud rds instances set-deletion-protection ewdb-0123456789ab --enabled
```

To turn it off: `--no-enabled`.

**API**
```http
PATCH /rds/api/instances/ewdb-0123456789ab/deletion-protection
{ "enabled": true }
```

**Terraform**
```hcl
resource "dcloud_db_instance" "orders" {
  identifier          = "orders"
  instance_class      = "performance-2"
  deletion_protection = true
}
```

You can also turn it on at creation (`deletionProtection: true`).

## Delete the database

**Console**
1. Open the database, then choose **Danger zone** (or, in the **⋯** menu, **Delete database…**).
2. Under **Delete this database**, select or clear **Take a final backup and keep the backups for
   N days**. The check box is there, and selected, only while the database is `available` with
   backups on.
3. Choose **Delete database**.
4. In the dialog, enter the database name, and choose **Delete database**.

The dialog says what is removed and what is kept, and what keeping the backups costs. The console
returns to the database list.

**CLI**
```sh
dcloud rds instances delete ewdb-0123456789ab --final-backup true
```

**API**
```http
DELETE /rds/api/instances/ewdb-0123456789ab?finalBackup=true
```

Without `?finalBackup=true`, nothing is kept.

**Terraform**
Remove the `dcloud_db_instance` resource and run `terraform apply`. Terraform deletes without a final
backup.

## Result

The request answers with the status `deleting`. Branch pools of the database, and their branches,
are deleted with it.

**Without a final backup**, the machines, their volumes, the private address (a cluster's internal
network; your own network stays), the firewall, for a cluster the load balancer, the master
password and login passwords in **Secrets** and the backup repository in object storage are all
removed.

**With a final backup:**

1. A full backup is taken first and waited for, for up to two hours. If it fails or takes longer,
   nothing is deleted: the database is `available` again, and **Activity** says "Not deleted: the
   final backup … The database is available again."
2. The machines, volumes, address, firewall and load balancer are then removed. The backups, their
   encryption key, the master password and the login passwords are kept.
3. The database stays listed under its own name with status **Deleted, backups kept** and the date
   they go, for its backup retention period (`backupRetentionDays`, at least one day, counted
   from the final backup). Its page has **Backups**,
   **Activity** and **Danger zone** only, and All resources lists it the same way. In the API its
   status is `retained` and its identifier gets a `-deleted-…` suffix, so the original name is
   free at once; the console shows the original name and asks for it to remove the backups.

Every backup of the window is kept, and the ones you took with **Back up now** stay until you
delete them (on databases that keep them; see [Back up a database](back-up-a-database.md)). Backup
storage up to the database's allocated size is free; beyond it, the backup rate per GiB-month
(`rds.backup.gb_month`).

Restore from it like any database (see [Restore a database](restore-a-database.md)): to the final
backup, or to any moment its backups still cover. When the retention period ends, the backups and
the row are removed, unless it still holds backups taken with **Back up now**: then it stays until
you delete those. To remove everything sooner, delete the retained database again (**Remove the kept
backups**, then **Remove backups**); that removes everything at once.

The backup repository is the database's own bucket, and deleting the database empties and deletes
it. A delete that failed before it reached storage does not leave the bucket behind for good:
every ten minutes the platform removes any bucket of this installation that no database claims and
that is older than an hour.

## Related

- [Restore a database](restore-a-database.md)
- `rds.instances.delete` · `dcloud rds instances delete` · `dcloud_db_instance`
