Fixing 404 error of word press website without using plugin

My website Cheap Accountants in London was hit by a cyber attack. As a result, many spammy pages are created and being indexed by Google. I’ve deleted all those pages but they’re still indexed. And I’m also getting traffic on those pages. So in order to convert this traffic to my website, I’m planning to redirect all spammy pages to the home page. Is there a way to do it by manual coding? I don’t want to install the plugin.

why would you redirect from the spammy pages to your home page? it will lose your credibility, I think so,

I have deleted the spammy pages. Now it’s giving 404 error as these pages are still indexed by Google.

did you try reaching out WordPress? because if it happened there, that’s a security issue for them,

I was checking your repo a while ago, and I think it’s not on GitHub,