I would like to share how you can simply remove a domain from the system with this simple function.
You'll still need to remove the recordings and voicemails.
SQL:
CREATE OR REPLACE FUNCTION delete_domain(id UUID)
RETURNS void AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT table_schema, table_name
FROM information_schema.columns
JOIN information_schema.tables using(table_schema, table_name)
WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
AND table_schema = 'public' AND table_name like 'v_%' AND column_name = 'domain_uuid' AND table_name <> 'v_domains'
LOOP
RAISE NOTICE 'deleting % for %', quote_ident(r.table_schema) || '.' || quote_ident(r.table_name), id;
EXECUTE format('DELETE FROM %I.%I WHERE domain_uuid = $1', r.table_schema, r.table_name) USING id;
END LOOP;
EXECUTE 'DELETE FROM public.v_domains WHERE domain_uuid = $1' USING id;
END;
$$ LANGUAGE plpgsql;
You'll still need to remove the recordings and voicemails.