diff --git a/spydrnet/ir/instance.py b/spydrnet/ir/instance.py
index 33b9d8b..2f739de 100644
--- a/spydrnet/ir/instance.py
+++ b/spydrnet/ir/instance.py
@@ -50,11 +50,14 @@ class Instance(Element):
                     pin._instance = None
                     pin._inner_pin = None
             self._pins.clear()
+            if self._reference:
+                self._reference._references.remove(self)
         else:
             if self._reference is not None:
                 assert len(self.reference.ports) == len(value.ports) and all(len(x.pins) == len(y.pins) for x, y in
                                                                              zip(self.reference.ports, value.ports)), \
                     "Reference reassignment only supported for definitions with matching port positions"
+                self._reference._references.remove(self)
                 for cur_port, new_port in zip(self.reference.ports, value.ports):
                     for cur_pin, new_pin in zip(cur_port.pins, new_port.pins):
                         outer_pin = self._pins.pop(cur_pin)
@@ -67,6 +70,10 @@ class Instance(Element):
             value._references.add(self)
         self._reference = value
 
+    @reference.deleter
+    def reference(self):
+        self.reference = None
+
     @property
     def pins(self):
         '''get the pins on this instance.'''

Reference tracking added to definitions, but references not removed when instance reference reassigned or deleted
diff --git a/spydrnet/ir/tests/test_instance.py b/spydrnet/ir/tests/test_instance.py
index 3a74be5..0bf268a 100644
--- a/spydrnet/ir/tests/test_instance.py
+++ b/spydrnet/ir/tests/test_instance.py
@@ -116,6 +116,7 @@ class TestInstance(unittest.TestCase):
         pin1 = port.create_pin()
         pin2 = port.create_pin()
         self.instance.reference = definition
+        self.assertTrue(self.instance in definition.references)
         outer_pin1 = self.instance.pins[pin1]
         outer_pin2 = self.instance.pins[pin2]
 
@@ -124,5 +125,25 @@ class TestInstance(unittest.TestCase):
         pin1 = port.create_pin()
         pin2 = port.create_pin()
         self.instance.reference = definition2
+        self.assertTrue(self.instance not in definition.references)
+        self.assertTrue(self.instance in definition2.references)
         self.assertEqual(outer_pin1, self.instance.pins[pin1])
-        self.assertEqual(outer_pin2, self.instance.pins[pin2])
\ No newline at end of file
+        self.assertEqual(outer_pin2, self.instance.pins[pin2])
+
+    def test_reference_removal(self):
+        definition = sdn.Definition()
+        port = definition.create_port()
+        pin1 = port.create_pin()
+        pin2 = port.create_pin()
+        self.instance.reference = definition
+        self.assertTrue(self.instance in definition.references)
+        outer_pin1 = self.instance.pins[pin1]
+        outer_pin2 = self.instance.pins[pin2]
+        self.assertEqual(outer_pin1.inner_pin, pin1)
+        self.assertEqual(outer_pin2.inner_pin, pin2)
+
+        del self.instance.reference
+        self.assertTrue(self.instance not in definition.references)
+        self.assertIsNone(outer_pin1.inner_pin)
+        self.assertIsNone(outer_pin2.inner_pin)
+

pip install -e .[all]
parse_log_pytest
requirements.txt
pytest-cov pytest
apt-get update apt-get install -y gcc
3.7
requirements/base.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
coverage==7.2.7
exceptiongroup==1.2.2
importlib-metadata==6.7.0
iniconfig==2.0.0
packaging==24.0
pluggy==1.2.0
pytest==7.4.4
pytest-cov==4.1.0
-e git+https://github.com/byuccl/spydrnet.git@5c5a891e246a31a846e59efdf164ca99568d77a1#egg=spydrnet
tomli==2.0.1
typing_extensions==4.7.1
zipp==3.15.0

name: spydrnet
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
  - certifi=2022.12.7=py37h06a4308_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=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - coverage==7.2.7
      - exceptiongroup==1.2.2
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - packaging==24.0
      - pluggy==1.2.0
      - pytest==7.4.4
      - pytest-cov==4.1.0
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - zipp==3.15.0
prefix: /opt/conda/envs/spydrnet

spydrnet/ir/tests/test_instance.py::TestInstance::test_reference_reassignment spydrnet/ir/tests/test_instance.py::TestInstance::test_reference_removal
spydrnet/ir/tests/test_instance.py::TestInstance::test_constructor spydrnet/ir/tests/test_instance.py::TestInstance::test_post_assignment_pin_and_port_creation spydrnet/ir/tests/test_instance.py::TestInstance::test_post_assignment_pin_and_port_removal spydrnet/ir/tests/test_instance.py::TestInstance::test_reference_assignment spydrnet/ir/tests/test_instance.py::TestInstance::test_reference_assignment_with_pins
spydrnet/ir/instance.py
spydrnet/ir/instance.py