You can sell while a tenant is living in the house, but the sale itself does not cancel the lease or rental agreement. The buyer takes the property subject to it and gets the same rights to collect rent and enforce the lease. Share the lease and occupancy information so the buyer and closing team can plan the handoff. California Civil Code § 821.
You have two routes for the security deposit
When your ownership ends, you can transfer the remaining deposit to the buyer after lawful deductions. Notify the tenant by personal delivery or first-class mail of the transfer, any claims against the deposit, the amount transferred, and the buyer's name, address, and phone number.
Or return the remaining deposit to the tenant with an itemized accounting. If these steps are skipped, the buyer can share liability for the deposit. The sale handoff should account for each deposit and the records behind it. California Civil Code § 1950.5(i)–(k).
Plan for the new owner to give tenants its contact and rent-payment information within 15 days after the ownership change. Local rent and eviction rules may also apply. California Civil Code § 1962.
Keep the lease, deposit balance and records, and tenant notices together for the title-company closing. A sale does not by itself set a move-out date or make the existing lease disappear.