Yes, you have to file a tax return whether you are resident or non‑resident in Spain. However, the type of form and the deadline for filing differ.
Residents: they file a tax return (IRPF - Impuesto sobre la Renta de las Personas Físicas) that includes all the resident's income, including rental income.
Non‑residents: They file a tax return using Form 210 (Modelo 210, or IRNR - Impuesto sobre la Renta de No Residentes). The tax return is filed quarterly. From the year 2024, non‑residents can only file annually.
The filing deadline is the 20th day of the following year. This means that for rentals in the year 2025, the tax return must be filed by 20th of January, 2026.