@@ -176,26 +176,43 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
176176 return fmt .Errorf ("failed to determine Route 53 hosted zone ID: %w" , err )
177177 }
178178
179- records , err := d .getExistingRecordSets (ctx , hostedZoneID , info .EffectiveFQDN )
179+ existingRecords , err := d .getExistingRecordSets (ctx , hostedZoneID , info .EffectiveFQDN )
180180 if err != nil {
181181 return fmt .Errorf ("route53: %w" , err )
182182 }
183183
184- if len (records ) == 0 {
184+ if len (existingRecords ) == 0 {
185185 return nil
186186 }
187187
188+ var nonLegoRecords []awstypes.ResourceRecord
189+ for _ , record := range existingRecords {
190+ if deref (record .Value ) != `"` + info .Value + `"` {
191+ nonLegoRecords = append (nonLegoRecords , record )
192+ }
193+ }
194+
195+ action := awstypes .ChangeActionUpsert
196+
188197 recordSet := & awstypes.ResourceRecordSet {
189198 Name : aws .String (info .EffectiveFQDN ),
190199 Type : "TXT" ,
191200 TTL : aws .Int64 (int64 (d .config .TTL )),
192- ResourceRecords : records ,
201+ ResourceRecords : nonLegoRecords ,
193202 }
194203
195- err = d .changeRecord (ctx , awstypes .ChangeActionDelete , hostedZoneID , recordSet )
204+ // If the records are only records created by lego.
205+ if len (nonLegoRecords ) == 0 {
206+ action = awstypes .ChangeActionDelete
207+
208+ recordSet .ResourceRecords = existingRecords
209+ }
210+
211+ err = d .changeRecord (ctx , action , hostedZoneID , recordSet )
196212 if err != nil {
197213 return fmt .Errorf ("route53: %w" , err )
198214 }
215+
199216 return nil
200217}
201218
0 commit comments