@@ -769,11 +769,19 @@ func (m *Meta) backendFromConfig(opts *BackendOpts) (backend.Backend, tfdiags.Di
769769 s .StateStore .Provider .Source .Type ,
770770 s .StateStore .Provider .Source ,
771771 )
772- return nil , diags .Append (& hcl.Diagnostic {
773- Severity : hcl .DiagError ,
774- Summary : "Not implemented yet" ,
775- Detail : "Unsetting a state store is not implemented yet" ,
776- })
772+
773+ initReason := fmt .Sprintf ("Unsetting the previously set state store %q" , s .StateStore .Type )
774+ if ! opts .Init {
775+ diags = diags .Append (errStateStoreInitDiag (initReason ))
776+ return nil , diags
777+ }
778+
779+ if ! m .migrateState {
780+ diags = diags .Append (migrateOrReconfigStateStoreDiag )
781+ return nil , diags
782+ }
783+
784+ return m .stateStore_c_S (sMgr , opts .ViewType )
777785
778786 // Configuring a backend for the first time or -reconfigure flag was used
779787 case backendConfig != nil && s .Backend .Empty () &&
@@ -1851,6 +1859,66 @@ func (m *Meta) stateStore_C_s(c *configs.StateStore, stateStoreHash int, backend
18511859 return b , diags
18521860}
18531861
1862+ // Unconfiguring a state store (moving from state store => local).
1863+ func (m * Meta ) stateStore_c_S (ssSMgr * clistate.LocalState , viewType arguments.ViewType ) (backend.Backend , tfdiags.Diagnostics ) {
1864+ var diags tfdiags.Diagnostics
1865+
1866+ s := ssSMgr .State ()
1867+ stateStoreType := s .StateStore .Type
1868+
1869+ m .Ui .Output (fmt .Sprintf (strings .TrimSpace (outputStateStoreMigrateLocal ), stateStoreType ))
1870+
1871+ // Grab a purely local backend to get the local state if it exists
1872+ localB , moreDiags := m .Backend (& BackendOpts {ForceLocal : true , Init : true })
1873+ diags = diags .Append (moreDiags )
1874+ if moreDiags .HasErrors () {
1875+ return nil , diags
1876+ }
1877+
1878+ // Initialize the configured state store
1879+ ss , moreDiags := m .savedStateStore (ssSMgr )
1880+ diags = diags .Append (moreDiags )
1881+ if moreDiags .HasErrors () {
1882+ return nil , diags
1883+ }
1884+
1885+ // Perform the migration
1886+ err := m .backendMigrateState (& backendMigrateOpts {
1887+ SourceType : stateStoreType ,
1888+ DestinationType : "local" ,
1889+ Source : ss ,
1890+ Destination : localB ,
1891+ ViewType : viewType ,
1892+ })
1893+ if err != nil {
1894+ diags = diags .Append (err )
1895+ return nil , diags
1896+ }
1897+
1898+ // Remove the stored metadata
1899+ s .StateStore = nil
1900+ if err := ssSMgr .WriteState (s ); err != nil {
1901+ diags = diags .Append (errStateStoreClearSaved {err })
1902+ return nil , diags
1903+ }
1904+ if err := ssSMgr .PersistState (); err != nil {
1905+ diags = diags .Append (errStateStoreClearSaved {err })
1906+ return nil , diags
1907+ }
1908+
1909+ switch viewType {
1910+ case arguments .ViewHuman :
1911+ m .Ui .Output (m .Colorize ().Color (fmt .Sprintf (
1912+ "[reset][green]\n \n " +
1913+ strings .TrimSpace (successStateStoreUnset ), stateStoreType )))
1914+ case arguments .ViewJSON :
1915+ // TODO: Implement JSON output for ViewJSON
1916+ }
1917+
1918+ // Return no state store
1919+ return nil , diags
1920+ }
1921+
18541922// getStateStorageProviderVersion gets the current version of the state store provider that's in use. This is achieved
18551923// by inspecting the current locks.
18561924//
@@ -2580,6 +2648,10 @@ const outputBackendMigrateLocal = `
25802648Terraform has detected you're unconfiguring your previously set %q backend.
25812649`
25822650
2651+ const outputStateStoreMigrateLocal = `
2652+ Terraform has detected you're unconfiguring your previously set %q state store.
2653+ `
2654+
25832655const outputBackendReconfigure = `
25842656[reset][bold]Backend configuration changed![reset]
25852657
@@ -2600,6 +2672,10 @@ const successBackendUnset = `
26002672Successfully unset the backend %q. Terraform will now operate locally.
26012673`
26022674
2675+ const successStateStoreUnset = `
2676+ Successfully unset the state store %q. Terraform will now operate locally.
2677+ `
2678+
26032679const successBackendSet = `
26042680Successfully configured the backend %q! Terraform will automatically
26052681use this backend unless the backend configuration changes.
0 commit comments