diff --git a/CHANGES.rst b/CHANGES.rst
index da7f937..6a64f71 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -36,6 +36,9 @@
 - Remove HST-specific WCS keywords from SCI extensions that, following HST
   convention, are supposed to be present in primary header only. [#159]
 
+- Do not archive primary WCS when applying headerlet as primary if distortion
+  model changes. [#161]
+
 
 1.5.3 (2019-09-23)
 ------------------
diff --git a/stwcs/wcsutil/headerlet.py b/stwcs/wcsutil/headerlet.py
index fcb268b..53a26ce 100644
--- a/stwcs/wcsutil/headerlet.py
+++ b/stwcs/wcsutil/headerlet.py
@@ -1997,12 +1997,11 @@ class Headerlet(fits.HDUList):
                     altwcs.archive_wcs(fobj, ext=sciext_list, mode=mode)
 
             else:
-                # explicity pass wcsname so that if a WCS with the same name
-                # already exists, overwrite it in place:
-                pri_wcsname = scihdr.get('WCSNAME', None)
-                altwcs.archive_wcs(fobj, ext=sciext_list, wcsname=pri_wcsname, mode=mode)
+                # Add primary WCS to the list of WCS to be saved to headerlets:
+                all_wcs_dict = alt_wcs_names_dict.copy()
+                all_wcs_dict[' '] = scihdr.get('WCSNAME', ' ')
 
-                for wcskey, hname in alt_wcs_names_dict:
+                for wcskey, hname in all_wcs_dict.items():
                     if hname not in hdrlet_extnames:
                         # create HeaderletHDU for alternate WCS now
                         alt_hlet = create_headerlet(fobj, sciext=sciext_list,
@@ -2017,6 +2016,8 @@ class Headerlet(fits.HDUList):
                         alt_hlethdu.append(alt_hlet_hdu)
                         hdrlet_extnames.append(hname)
 
+                    altwcs.deleteWCS(fobj, sciext_list, wcskey=wcskey, wcsname=hname)
+
         self._del_dest_WCS_ext(fobj)
         for i in range(1, numsip + 1):
             target_ext = sciext_list[i - 1]

Applying a headerlet should remove all alternate WCSs if distortions are different
When a headerlet from the astrometry database is applied to an observation and its distortion is different from the one in the header the primary WCS is archived as alternate which essentially links it to the new distortion. This is incorrect. All alternate WCSs should be removed (possibly attached as headerlets) when a different distortion model is applied.

@stsci-hack Should the fix go in 1.6 or 1.7?
diff --git a/stwcs/tests/test_altwcs.py b/stwcs/tests/test_altwcs.py
index d5fd39a..378f060 100644
--- a/stwcs/tests/test_altwcs.py
+++ b/stwcs/tests/test_altwcs.py
@@ -257,15 +257,14 @@ class TestAltWCS(object):
         h = pyfits.open(self.acs_file, mode='update')
         for ext in ext_list:
             wnames = altwcs.wcsnames(h, ext=ext)
-            assert not set(altwcs.wcskeys(h, ext)).symmetric_difference(['O', ' ', 'A', 'B'])
-            assert wnames['A'].upper() == 'IDC_POSTSM4'
+            assert not set(altwcs.wcskeys(h, ext)).symmetric_difference(['O', ' ', 'A'])
+            assert wnames['A'].upper() == 'IDC_0461802EJ'
             assert '-FIT' in wnames[' '].upper() and 'IDC_0461802EJ' in wnames[' '].upper()
             assert not np.allclose(h[ext].header['CD1_1'], ext[1] + 0.234)
             assert 'HDRNAME' in h[ext].header
 
             # check that original primary WCS is stored under key 'A':
             wcsa = wcsutil.HSTWCS(h, ext, wcskey='A')
-            compare_wcs(ref_priwcs[ext], wcsa)
 
         h.close()
 

pip install -e .[all]
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc
3.9
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
astropy==6.0.1
astropy-iers-data==0.2025.3.31.0.36.18
certifi==2025.1.31
charset-normalizer==3.4.1
exceptiongroup==1.2.2
idna==3.10
iniconfig==2.1.0
lxml==5.3.1
numpy==1.26.4
packaging==24.2
pluggy==1.5.0
pyerfa==2.0.1.5
pytest==8.3.5
PyYAML==6.0.2
requests==2.32.3
stsci.tools==4.2.0
-e git+https://github.com/spacetelescope/stwcs.git@4f1eb09a6dd03f8e9e9f085d781933ff7fa36cad#egg=stwcs
tomli==2.2.1
urllib3==2.3.0

name: stwcs
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - astropy==6.0.1
      - astropy-iers-data==0.2025.3.31.0.36.18
      - certifi==2025.1.31
      - charset-normalizer==3.4.1
      - exceptiongroup==1.2.2
      - idna==3.10
      - iniconfig==2.1.0
      - lxml==5.3.1
      - numpy==1.26.4
      - packaging==24.2
      - pluggy==1.5.0
      - pyerfa==2.0.1.5
      - pytest==8.3.5
      - pyyaml==6.0.2
      - requests==2.32.3
      - stsci-tools==4.2.0
      - tomli==2.2.1
      - urllib3==2.3.0
prefix: /opt/conda/envs/stwcs

stwcs/tests/test_altwcs.py::TestAltWCS::test_repeated_updatewcs_use_db
stwcs/tests/test_altwcs.py::TestAltWCS::test_archive stwcs/tests/test_altwcs.py::TestAltWCS::test_archive_clobber stwcs/tests/test_altwcs.py::TestAltWCS::test_restore_wcs stwcs/tests/test_altwcs.py::TestAltWCS::test_restore_wcs_mem stwcs/tests/test_altwcs.py::TestAltWCS::test_restore_simple stwcs/tests/test_altwcs.py::TestAltWCS::test_restore_wcs_from_to stwcs/tests/test_altwcs.py::TestAltWCS::test_delete_wcs stwcs/tests/test_altwcs.py::TestAltWCS::test_pars_file_mode1 stwcs/tests/test_altwcs.py::TestAltWCS::test_pars_file_mode2 stwcs/tests/test_altwcs.py::TestAltWCS::test_pars_ext stwcs/tests/test_altwcs.py::TestAltWCS::test_pars_wcskey_not1char stwcs/tests/test_altwcs.py::TestAltWCS::test_pars_wcskey stwcs/tests/test_altwcs.py::TestAltWCS::test_repeated_updatewcs_no_db
CHANGES.rst stwcs/wcsutil/headerlet.py
CHANGES.rst stwcs/wcsutil/headerlet.py