Index: /branches/rel_avx_2_7_4/scripts/chrony_setup.py
===================================================================
--- /branches/rel_avx_2_7_4/scripts/chrony_setup.py	(revision 9002)
+++ /branches/rel_avx_2_7_4/scripts/chrony_setup.py	(working copy)
@@ -170,28 +170,43 @@
         subprocess.call(cmd, env=CHRONY_ENV)
 
 def remove_key(key_id):
-    """Remove a KEY entry from the chrony keys file and dynamically using chronyc."""
+    """Remove a KEY entry from the chrony keys file and remove related NTP servers using that key."""
+    # Step 1: Remove key entry from chrony.keys
     with open(CHRONY_KEYS_FILE, "r") as f:
         lines = f.readlines()
 
-    # Filter out lines that match the key_id
     updated_lines = []
     for line in lines:
         if line.strip().startswith("#") or not line.strip():
             updated_lines.append(line)
             continue
-
         if line.split()[0] != key_id:
             updated_lines.append(line)
 
     with open(CHRONY_KEYS_FILE, "w") as f:
         f.writelines(updated_lines)
 
-    # Dynamically delete from chrony if daemon is running
+    # Step 2: Remove servers/pools using that key ID from chrony.conf
+    if os.path.exists(CHRONY_CONFIG_FILE):
+        with open(CHRONY_CONFIG_FILE, "r") as f:
+            conf_lines = f.readlines()
+
+        new_conf_lines = []
+        for line in conf_lines:
+            line_strip = line.strip()
+            if (line_strip.startswith("server") or line_strip.startswith("pool")) and f"key {key_id}" in line_strip:
+                continue  # skip lines using the deleted key
+            new_conf_lines.append(line)
+
+        with open(CHRONY_CONFIG_FILE, "w") as f:
+            f.writelines(new_conf_lines)
+
+    # Step 3: Reload chrony if it's running
     if os.path.exists("/var/run/chrony/chronyd.pid"):
         cmd = ["chronyc", "reload", "sources"]
         subprocess.call(cmd, env=CHRONY_ENV)
 
+
 if __name__ == "__main__":
     parser = argparse.ArgumentParser(description="Manage chrony service and configuration.")
     parser.add_argument("-s", "--start", action="store_true", help="Start the chrony service.")
