Query to remove all databases from AG

  • As part of  DR we need to remove all the  databases  from  different  AG's  , each AG  is consists of 20+ databases ,  i need a query to  generate script for removing all databases  with  respect to specific AG group , .

     

    Thanks,.

  • So you want to query the DMVs sys.availability_groups, sys.dm_hadr_database_replica_states and sys.databases.

    These 3 will get you the databases per AG.

    From there you can then write a loop or whatever based approach you prefer to then dynamically build the ALTER DATABASE SET HADR OFF command to remove that DB from the replica.

  • Thanks for the  suggestion and reply ,  please  provide  the query   as  i am not proficient in programming

  • Please have a go and try, the more you try the better you will become.

    Post the query you use and it will show initiative that you have at least tried.

    It will help you both personally and professionally that you try without being handed the answer.

    First start by getting the list of databases for the AG then build out into the removal stage.

  • Or if you don’t want to do it yourself, look at DBATools and Remove-DbaAgDatabase which will do it all for you.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply