I transferred money from my checking to my HSA and now I realize I need it back but not for medical expenses. I usually do direct deposits but I needed some of that money for medical because my company had not deposited their half yet. Can I transfer it back when my company deposits their half?
You can (and definitely should) withdraw any part of the contribution that will put you over the contribution limit. You can (and should if you need to) withdraw to repay any medical payments you made from outside the account. You can (but should avoid at all costs) withdraw (distribute) from the HSA for non-medical reasons.
Here's the IRS publication which covers this: https://www.irs.gov/publications/p969
Here's the bit about distributions that covers what you're trying to do:
You can receive tax-free distributions from your HSA to pay or be reimbursed for qualified medical expenses you incur after you establish the HSA. If you receive distributions for other reasons, the amount you withdraw will be subject to income tax and may be subject to an additional 20% tax. You don’t have to make distributions from your HSA each year.
It is better to pay from your checking and reimburse than to over-fund the HSA. Best route forward is to reduce your contributions for the rest of the year, especially if continuing them will cause excess contributions. Another nasty gotcha:
Excess contributions. You will have excess contributions if the contributions to your HSA for the year are greater than the limits discussed earlier. Excess contributions aren’t deductible. Excess contributions made by your employer are included in your gross income. If the excess contribution isn’t included in box 1 of Form W-2, you must report the excess as "Other income" on your tax return.
Generally, you must pay a 6% excise tax on excess contributions. See Form 5329, Additional Taxes on Qualified Plans (Including IRAs) and Other Tax-Favored Accounts, to figure the excise tax. The excise tax applies to each tax year the excess contribution remains in the account.
You may withdraw some or all of the excess contributions and avoid paying the excise tax on the amount withdrawn if you meet the following conditions.
•You withdraw the excess contributions by the due date, including extensions, of your tax return for the year the contributions were made.
•You withdraw any income earned on the withdrawn contributions and include the earnings in "Other income" on your tax return for the year you withdraw the contributions and earnings.
If you will not be over your maximum contribution, let the contribution ride. Make sure your HSA balance is divided between cash, stock fund, bond fund. Much like your 401k. Because the part that you don't spend on medical expenses this year can be spent in future years' medical expenses, and if you have anything left when you retire you can spend it on whatever you want. And the funds, including growth, are not taxed until you distribute them.
Bottom line: if the funds will not cause excess contribution, leave them in. Otherwise, take them out as soon as possible.