This page describes how to protect Spanner databases from accidental deletion.
Spanner database deletion protection prevents the accidental deletion of existing databases by users or service accounts that have the necessary IAM permissions to delete the database. By enabling database deletion protection, you can safeguard databases that are important to your application and services. Use database deletion protection along with point-in-time recovery and backup features to provide a comprehensive set of data protection capabilities for your Spanner databases.
By default, the deletion protection setting is disabled when you create your new database. You can enable the deletion protection setting after the database creation succeeds. Additionally, you can enable this setting on an existing database. If you want to protect multiple databases, enable the setting on each database individually. Enabling or disabling deletion protection doesn't have any performance impact on the database. If you need to delete a database that has database protection enabled, you need to disable the protection before you can delete the database.
Limitations
You can't enable database deletion protection in the following scenarios:
- If the database is being deleted.
- If the database is being restored from a backup. (After the restore operation completes, you can enable database protection).
In addition, backups of a database and databases restored from a backup don't inherit the database deletion protection setting of their source database. After you restore a database from a backup, you must enable its database deletion protection separately.
If you delete your project, Spanner database deletion protection doesn't prevent the deletion of your database or instance. For more information about what happens when you delete your project, see Shutting down (deleting) projects.
Access control with IAM
To enable the deletion protection setting of your database, you must have certain IAM permissions.
You need to have the spanner.databases.update
permission to enable or
disable database deletion protection. If you only need to view the status of
your database configuration, you need to have the spanner.databases.list
or
spanner.databases.get
permission. For information on how to grant
Spanner IAM permissions, see
Apply IAM permissions.
If you have the predefined Spanner Database Admin
roles/spanner.databaseAdmin
role for
your database, you can update and enable database deletion protection.
You can enable the database deletion protection setting on an existing database to prevent the accidental deletion of the database.
Enable database deletion protection
You can enable database deletion protection using the gcloud CLI, the client libraries, and the REST or RPC APIs. You can't enable database deletion protection using the Google Cloud console.
gcloud
To enable the deletion protection setting of a database, run the following command:
gcloud spanner databases update
DATABASE_ID --instance=INSTANCE_ID
--enable-drop-protection [--async]
The following options are required:
DATABASE_ID
- ID of the database.
INSTANCE_ID
- ID of the instance for the database.
The following options are optional:
--async
- Return immediately, without waiting for the operation in progress to complete.
Client libraries
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
Check if a database has deletion protection enabled
You can determine if your database has its deletion protection enabled by viewing the database configuration.
gcloud
To check if a database has deletion protection enabled, you can run the
gcloud spanner databases describe
command to get detailed information
about a database, or you can run the gcloud spanner databases list
to
get detailed information about databases within an instance.
gcloud spanner databases describe
projects/PROJECT_ID/instances/INSTANCE_ID/databases/DATABASE_ID
The following options are required:
PROJECT_ID
- ID of the project for the database.
INSTANCE_ID
- ID of the instance for the database.
DATABASE_ID
- ID of the database.
If deletion protection is enabled, you'll see an
enableDropProtection: true
parameter in the output.
Disable database deletion protection
You can disable database deletion protection if a database no longer needs this protection or if you need to delete a database that has this setting enabled.
If you want to delete an instance that has one or more databases with deletion protection enabled, you must first disable the deletion protection on all databases in that instance before you can delete the instance.
gcloud
To disable the deletion protection setting of a database, run the following command:
gcloud spanner databases update
DATABASE_ID --instance=INSTANCE_ID
--no-enable-drop-protection [--async]
The following options are required:
DATABASE_ID
- ID of the database.
INSTANCE_ID
- ID of the instance for the database.
The following options are optional:
--async
- Return immediately, without waiting for the operation in progress to complete.
What's next
- Learn how to Create and manage databases.
- Learn more about how to backup and restore a database.
- Learn how to make schema updates.