DeleteInstance permanently purges a soft-deleted instance and all related resources. This operation is irreversible and should only be used for: - Administrative cleanup of old soft-deleted instances - Test cleanup Following AIP-164/165, this only works on instances where deleted = TRUE.
(ctx context.Context, workspace string, resourceID string)
| 565 | // - Test cleanup |
| 566 | // Following AIP-164/165, this only works on instances where deleted = TRUE. |
| 567 | func (s *Store) DeleteInstance(ctx context.Context, workspace string, resourceID string) error { |
| 568 | tx, err := s.GetDB().BeginTx(ctx, nil) |
| 569 | if err != nil { |
| 570 | return errors.Wrap(err, "failed to begin transaction") |
| 571 | } |
| 572 | defer tx.Rollback() |
| 573 | |
| 574 | // Delete query_history entries that reference this instance |
| 575 | // The database field contains instance reference like 'instances/{instance}/databases/{database}' |
| 576 | q := qb.Q().Space(` |
| 577 | DELETE FROM query_history |
| 578 | WHERE database LIKE 'instances/' || ? || '/databases/%' |
| 579 | `, resourceID) |
| 580 | query, args, err := q.ToSQL() |
| 581 | if err != nil { |
| 582 | return errors.Wrapf(err, "failed to build sql") |
| 583 | } |
| 584 | if _, err := tx.ExecContext(ctx, query, args...); err != nil { |
| 585 | return errors.Wrapf(err, "failed to delete query_history for instance %s", resourceID) |
| 586 | } |
| 587 | |
| 588 | // Delete task_run_log entries for tasks associated with this instance |
| 589 | q = qb.Q().Space(` |
| 590 | DELETE FROM task_run_log trl |
| 591 | USING task_run tr, task t |
| 592 | WHERE trl.project = tr.project |
| 593 | AND trl.task_run_id = tr.id |
| 594 | AND tr.project = t.project |
| 595 | AND tr.task_id = t.id |
| 596 | AND t.instance = ? |
| 597 | `, resourceID) |
| 598 | query, args, err = q.ToSQL() |
| 599 | if err != nil { |
| 600 | return errors.Wrapf(err, "failed to build sql") |
| 601 | } |
| 602 | if _, err := tx.ExecContext(ctx, query, args...); err != nil { |
| 603 | return errors.Wrapf(err, "failed to delete task_run_log for instance %s", resourceID) |
| 604 | } |
| 605 | |
| 606 | // Delete task_run entries for tasks associated with this instance |
| 607 | q = qb.Q().Space(` |
| 608 | DELETE FROM task_run tr |
| 609 | USING task t |
| 610 | WHERE tr.project = t.project |
| 611 | AND tr.task_id = t.id |
| 612 | AND t.instance = ? |
| 613 | `, resourceID) |
| 614 | query, args, err = q.ToSQL() |
| 615 | if err != nil { |
| 616 | return errors.Wrapf(err, "failed to build sql") |
| 617 | } |
| 618 | if _, err := tx.ExecContext(ctx, query, args...); err != nil { |
| 619 | return errors.Wrapf(err, "failed to delete task_run for instance %s", resourceID) |
| 620 | } |
| 621 | |
| 622 | // Delete tasks associated with this instance |
| 623 | q = qb.Q().Space(` |
| 624 | DELETE FROM task WHERE instance = ? |