There are circumstances where you can claim tax relief on accountancy fees relating to an HMRC enquiry but these are only allowed when an HMRC enquiry results in no adjustment to profits.
Any fees incurred handling an HMRC enquiry where there is an adjustment to profits which are not as a result of negligent or fraudulent conduct or careless behaviour are also allowable.
If you incur fees on an HMRC enquiry which uncovers errors as a result of your carelessness or wrongdoing these are not allowable.
See HMRC’s guidance notes here.
Legal fees are not tax deductible even where they are incurred as a result of a successful appeal against HMRC