    "create index catalogs_job_id_idx on catalogs(job_id) where job_id is not null"))
 
 (defn rededuplicate-facts []
-  (log/info (trs "[1/6] Creating new fact storage tables..."))
+  (log/info (trs "[1/7] Cleaning up unreferenced facts..."))
+  (jdbc/do-commands
+   "DELETE FROM facts WHERE factset_id NOT IN (SELECT id FROM factsets)")
+
+  (log/info (trs "[2/7] Creating new fact storage tables..."))
   (jdbc/do-commands
    "CREATE SEQUENCE fact_values_id_seq;"
 

       fact_value_id bigint NOT NULL
    );")

  (log/info (trs "[2/6] Copying unique fact values into fact_values"))
  (jdbc/do-commands
   "INSERT INTO fact_values (value, value_integer, value_float, value_string, value_boolean, value_type_id)
       SELECT distinct value, value_integer, value_float, value_string, value_boolean, value_type_id FROM facts")

        WHERE f.value IS NULL AND fv.value IS NULL")

  ;; populate fact_values.value_hash
  (log/info (trs "[5/7] Computing fact value hashes..."))
  (jdbc/call-with-query-rows
   ["select id, value::text from fact_values"]
   (fn [rows]
