Background Intracranial aneurysm (IA) is a cerebrovascular disease with a high mortality rate due to ruptured subarachnoid hemorrhage. While Krüppel-like factor 6 (KLF6) dysregulation has been implicated in cancer and cardiovascular diseases, its role in IA remains unclear. Materials and methods The GSE122897 and GSE15629 datasets were downloaded from the Gene Expression Omnibus database. Immune cell infiltration and hypoxia analysis were performed to explore the effects of KLF6 on IA. Weighted gene co-expression network analysis was used to identify hub genes related to KLF6 expression for subsequent analyses. Hypoxia-related genes were identified. Drug prediction was performed for IA. Samples from healthy individuals and patients with IA were collected to detect the expression of endothelin-1 (ET-1), vascular hematoma factor (vWF), and KLF6. A model of H2O2-induced human brain vascular smooth muscle cells (HBVSMC) injury was constructed to explore the effects of KLF6 and melatonin to treat IA. Results T cells CD4 memory resting and monocytes were significantly different in the KLF6 high and low expression groups. Four hypoxia-related gene sets were significantly enriched in the KLF6 high-expression group. Six hypoxia-related hub genes were obtained, which were significantly associated with KLF6. Drug prediction showed that melatonin may be a potential drug for IA. The levels of ET-1, vWF, and KLF6 were significantly upregulated in patients with IA. KLF6 exacerbates H2O2-induced injury in HBVSMC, ameliorated by melatonin. Conclusion KLF6 may be a potential target for IA treatment, with melatonin-mediated KLF6 effects playing a crucial role in the development of IA.