However it really depends on what your data is and what questions you are trying to ask. It may make more sense to filtre your data (i.e. subset it).
We probably need a REPREX (Create a reprex) with a statement of the issue and some sample data.
A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.
This first converts the gender variable to a factor and then removes the unused levels. It is also possible that there is no level with the value "Unknown/Invalid" in your data, in that case, the subset(data.all, gender != "Unknown/Invalid") will remove all the data, so it is better to check the levels of the variable before using the subset function.