1. He walked for ten miles.

2. He walked ten miles.
[Are both grammatical?]

3. He slept for seven hours.
4. He slept seven hours.
[Are both grammatical?]

5. I have lived here for five years.
6. I have lived here five years.
[What about this pair? can we use #6? Can we delete 'for' as in #6?]

Yes, all are fine. You can use these with or without “for.”

1. Yes, both sentences are grammatical. In sentence 1, "for" introduces the duration of time and emphasizes that the action of walking lasted for ten miles. In sentence 2, "for" is omitted, but the meaning remains the same. Both sentences convey the idea that he walked a distance of ten miles.

3. Yes, both sentences are grammatical. In sentence 3, "for" introduces the duration of time and emphasizes that the action of sleeping lasted for seven hours. In sentence 4, "for" is omitted, but the meaning remains the same. Both sentences convey the idea that he slept a duration of seven hours.

5. Yes, both sentences are grammatical. In sentence 5, "for" introduces the duration of time and emphasizes that the action of living here has lasted for five years. In sentence 6, "for" is omitted, but the meaning remains the same. Both sentences convey the idea that the speaker has lived in the same place for a duration of five years.