Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions spanner/backups/src/spanner_backups/delete_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START spanner_delete_backup]
import sys
import time

from google.api_core import exceptions
from google.cloud import spanner_admin_database_v1


def delete_backup(project_id: str, instance_id: str, backup_id: str) -> None:
"""Waits for referencing databases to finish optimizing and deletes a backup.

Args:
project_id: The Google Cloud project ID.
instance_id: The Spanner instance ID.
backup_id: The backup ID to delete.
"""
client = spanner_admin_database_v1.DatabaseAdminClient()
backup_path = client.backup_path(project_id, instance_id, backup_id)
get_request = spanner_admin_database_v1.GetBackupRequest(name=backup_path)
delete_request = spanner_admin_database_v1.DeleteBackupRequest(name=backup_path)

def wait_until_unreferenced() -> None:
deadline = time.monotonic() + 1800
backup = client.get_backup(request=get_request)
if backup.referencing_databases:
print(f"Waiting for backup '{backup_id}' to be unreferenced...")
while backup.referencing_databases:
if time.monotonic() > deadline:
raise TimeoutError("restored databases still reference the backup")
time.sleep(10)
backup = client.get_backup(request=get_request)
Comment on lines +36 to +45

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of wait_until_unreferenced loops indefinitely without providing any feedback to the user or having a timeout mechanism. Since database optimization after a restore can take a significant amount of time, a silent infinite loop can make the script appear frozen.

Adding a status message inside the loop and a timeout mechanism (e.g., 30 minutes) will improve usability and prevent the script from hanging indefinitely in CI/CD pipelines or local executions.

    def wait_until_unreferenced(timeout_seconds: int = 1800) -> None:
        start_time = time.time()
        backup = client.get_backup(request=get_request)
        while backup.referencing_databases:
            if time.time() - start_time > timeout_seconds:
                raise TimeoutError(
                    f"Timed out after {timeout_seconds} seconds waiting for backup to be unreferenced."
                )
            print(
                f"Backup is still referenced by {len(backup.referencing_databases)} database(s). "
                "Waiting 10 seconds...",
                file=sys.stderr,
            )
            time.sleep(10)
            backup = client.get_backup(request=get_request)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done: the wait now prints a progress message, stops after 30 minutes, and reports the timeout on stderr; a unit test covers it (1336509).


try:
wait_until_unreferenced()
client.delete_backup(request=delete_request)
except TimeoutError as err:
print(
f"error: timed out waiting to delete backup '{backup_id}' on "
f"instance '{instance_id}': {err}",
file=sys.stderr,
)
except exceptions.NotFound as err:
print(
f"error: backup '{backup_id}' on instance '{instance_id}' "
f"was not found: {err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to delete backup '{backup_id}' on instance "
f"'{instance_id}': {err.message}",
file=sys.stderr,
)
else:
print(f"Resource ID: {backup_id}")
print(f"Deleted Backup: {backup_id}")


# [END spanner_delete_backup]


if __name__ == "__main__":
import argparse
import os

parser = argparse.ArgumentParser(description="Delete a Spanner database backup.")
parser.add_argument(
"--project-id",
default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"),
)
parser.add_argument(
"--instance-id",
default=os.environ.get("SPANNER_INSTANCE_ID", "example-instance"),
)
parser.add_argument(
"--backup-id",
default=os.environ.get("SPANNER_DISPOSABLE_BACKUP_ID", "example-backup"),
)
args = parser.parse_args()

delete_backup(
project_id=args.project_id,
instance_id=args.instance_id,
backup_id=args.backup_id,
)
141 changes: 141 additions & 0 deletions spanner/backups/tests/delete_backup_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import runpy
from unittest import mock

from google.api_core import exceptions
import pytest
from spanner_backups import delete_backup

from . import conftest

PROJECT_ID = "example-project-id"
INSTANCE_ID = "example-instance"
BACKUP_ID = "example-backup"
ADMIN_CLIENT = "google.cloud.spanner_admin_database_v1.DatabaseAdminClient"


def _setup_admin(mock_client_class: mock.MagicMock) -> None:
client = mock_client_class.return_value
client.backup_path.return_value = (
f"projects/{PROJECT_ID}/instances/{INSTANCE_ID}/backups/{BACKUP_ID}"
)
client.get_backup.return_value.referencing_databases = []


@mock.patch(ADMIN_CLIENT)
def test_delete_backup_success(
mock_client_class: mock.MagicMock, capsys: pytest.CaptureFixture[str]
) -> None:
_setup_admin(mock_client_class)

delete_backup.delete_backup(PROJECT_ID, INSTANCE_ID, BACKUP_ID)

mock_client_class.return_value.delete_backup.assert_called_once()
out, err = capsys.readouterr()
assert f"Resource ID: {BACKUP_ID}" in out
assert f"Deleted Backup: {BACKUP_ID}" in out
assert PROJECT_ID not in out
assert err == ""


@pytest.mark.parametrize(
"error, expected_prefix",
[
(exceptions.NotFound("missing"), "error: backup 'example-backup'"),
(
exceptions.FailedPrecondition("in use"),
"error: failed to delete backup 'example-backup'",
),
],
)
@mock.patch(ADMIN_CLIENT)
def test_delete_backup_errors(
mock_client_class: mock.MagicMock,
capsys: pytest.CaptureFixture[str],
error: Exception,
expected_prefix: str,
) -> None:
_setup_admin(mock_client_class)
mock_client_class.return_value.delete_backup.side_effect = error

delete_backup.delete_backup(PROJECT_ID, INSTANCE_ID, BACKUP_ID)

_out, err = capsys.readouterr()
assert err.startswith(expected_prefix)
assert PROJECT_ID not in err


@mock.patch(ADMIN_CLIENT)
def test_delete_backup_times_out_while_referenced(
mock_client_class: mock.MagicMock, capsys: pytest.CaptureFixture[str]
) -> None:
_setup_admin(mock_client_class)
client = mock_client_class.return_value
client.get_backup.return_value.referencing_databases = ["restored-db"]

with mock.patch.object(delete_backup, "time") as mock_time:
mock_time.monotonic.side_effect = [0.0, 2000.0]
delete_backup.delete_backup(PROJECT_ID, INSTANCE_ID, BACKUP_ID)

client.delete_backup.assert_not_called()
out, err = capsys.readouterr()
assert f"Waiting for backup '{BACKUP_ID}' to be unreferenced..." in out
assert err.startswith(f"error: timed out waiting to delete backup '{BACKUP_ID}'")
assert PROJECT_ID not in err


@mock.patch(ADMIN_CLIENT)
def test_delete_backup_cli_entrypoint(
mock_client_class: mock.MagicMock,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
_setup_admin(mock_client_class)
monkeypatch.setattr(
"sys.argv",
[
"delete_backup.py",
"--project-id",
PROJECT_ID,
"--instance-id",
INSTANCE_ID,
"--backup-id",
BACKUP_ID,
],
)

runpy.run_module("spanner_backups.delete_backup", run_name="__main__")

assert f"Deleted Backup: {BACKUP_ID}" in capsys.readouterr().out


@conftest.live_only
def test_delete_backup_live(capsys: pytest.CaptureFixture[str]) -> None:
required = ("SPANNER_DISPOSABLE_BACKUP_ID",)
missing = [name for name in required if not os.environ.get(name)]
if missing:
pytest.skip(f"Set {', '.join(missing)} to run this live test.")
disposable_bkp = os.environ["SPANNER_DISPOSABLE_BACKUP_ID"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Accessing os.environ["SPANNER_DISPOSABLE_BACKUP_ID"] directly will raise a KeyError if the environment variable is not set. It is safer to use os.environ.get and skip the test gracefully if the variable is missing, especially since this is a live integration test.

    disposable_bkp = os.environ.get("SPANNER_DISPOSABLE_BACKUP_ID")
    if not disposable_bkp:
        pytest.skip("SPANNER_DISPOSABLE_BACKUP_ID environment variable not set.")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done: the live test now checks for its required environment variables up front and skips with a clear message when one is missing, instead of failing with a KeyError (1336509).

delete_backup.delete_backup(
conftest.LIVE_PROJECT_ID,
conftest.LIVE_INSTANCE_ID,
disposable_bkp,
)
out, err = capsys.readouterr()
assert err == ""
assert conftest.LIVE_PROJECT_ID not in out
assert f"Deleted Backup: {disposable_bkp}" in out