How to clean up the fetched repository by gitlab CI Runner when job is finished?

Every time when a job start running, the runner would fetch/pll the git repository, and it won’t delete the repository in runner when is finished, this would speed up the feching/pulling in next jobs, but we want to remove the repository for security reason as we use the shell executor, we can excute the “rm -rf .git *” at the after_script, but this will delete the artifacts too.

Is there any good practise to do the clean up?

I suppose you are using the “shell” CI executor

While I don’t know your exact situation, not use case - I can give you a link to one of my articles where I describe how you can setup VM runners via KVM (which is not natively supported by GitLab, but I haven’t had any issues so far. It works great); - mind you, you need a machine which will be running these VMs in the first place

Also, have you considered setting up docker executors?