2424import com .google .cloud .bigtable .admin .v2 .models .AppProfile ;
2525import com .google .cloud .bigtable .admin .v2 .models .Cluster ;
2626import com .google .cloud .bigtable .admin .v2 .models .Instance ;
27+ import com .google .cloud .bigtable .admin .v2 .models .LogicalView ;
28+ import com .google .cloud .bigtable .admin .v2 .models .MaterializedView ;
2729import com .google .cloud .bigtable .admin .v2 .models .Table ;
2830import com .google .cloud .bigtable .admin .v2 .models .UpdateAuthorizedViewRequest ;
31+ import com .google .cloud .bigtable .admin .v2 .models .UpdateLogicalViewRequest ;
32+ import com .google .cloud .bigtable .admin .v2 .models .UpdateMaterializedViewRequest ;
2933import com .google .cloud .bigtable .admin .v2 .models .UpdateTableRequest ;
3034import com .google .common .collect .ImmutableSet ;
3135import java .io .IOException ;
@@ -161,8 +165,6 @@ void cleanUpStale() throws ExecutionException, InterruptedException, IOException
161165 /**
162166 * Clean up AppProfile that were dynamically created in the default instance that have been
163167 * orphaned.
164- *
165- * @param stalePrefix
166168 */
167169 private void cleanupStaleTables (String stalePrefix ) {
168170 LOGGER .info ("Start cleaning up stale tables with stalePrefix=" + stalePrefix );
@@ -213,8 +215,6 @@ private void prepTableForDelete(String tableId) {
213215 /**
214216 * Clean up AppProfile that were dynamically created in the default instance that have been
215217 * orphaned.
216- *
217- * @param stalePrefix
218218 */
219219 private void cleanUpStaleAppProfile (String stalePrefix ) {
220220 for (AppProfile appProfile :
@@ -239,8 +239,6 @@ private void cleanUpStaleAppProfile(String stalePrefix) {
239239 /**
240240 * Clean up clusters that were dynamically created in the default instance that have been
241241 * orphaned.
242- *
243- * @param stalePrefix
244242 */
245243 private void cleanUpStaleClusters (String stalePrefix )
246244 throws ExecutionException , InterruptedException {
@@ -264,11 +262,36 @@ private void cleanUpStaleClusters(String stalePrefix)
264262 }
265263 }
266264
267- /**
268- * Clean up dynamically created (non-default) instances that have been orphaned.
269- *
270- * @param stalePrefix
271- */
265+ private void prepInstanceForDelete (String instanceId ) {
266+ if (env () instanceof EmulatorEnv ) {
267+ return ;
268+ }
269+ // Unprotected MaterializedViews.
270+ for (MaterializedView materializedView :
271+ env ().getInstanceAdminClient ().listMaterializedViews (instanceId )) {
272+ try {
273+ env ()
274+ .getInstanceAdminClient ()
275+ .updateMaterializedView (
276+ UpdateMaterializedViewRequest .of (instanceId , materializedView .getId ())
277+ .setDeletionProtection (false ));
278+ } catch (NotFoundException ignored ) {
279+ }
280+ }
281+ // Unprotected LogicalViews.
282+ for (LogicalView logicalView : env ().getInstanceAdminClient ().listLogicalViews (instanceId )) {
283+ try {
284+ env ()
285+ .getInstanceAdminClient ()
286+ .updateLogicalView (
287+ UpdateLogicalViewRequest .of (instanceId , logicalView .getId ())
288+ .setDeletionProtection (false ));
289+ } catch (NotFoundException ignored ) {
290+ }
291+ }
292+ }
293+
294+ /** Clean up dynamically created (non-default) instances that have been orphaned. */
272295 private void cleanUpStaleInstances (String stalePrefix )
273296 throws IOException , ExecutionException , InterruptedException {
274297 for (Instance instance : env ().getInstanceAdminClient ().listInstances ()) {
@@ -279,6 +302,7 @@ private void cleanUpStaleInstances(String stalePrefix)
279302 if (isNewerThanStale ) {
280303 continue ;
281304 }
305+ prepInstanceForDelete (instance .getId ());
282306 try {
283307 deleteInstance (instance .getId ());
284308 } catch (NotFoundException ignored ) {
0 commit comments