The way dogs think in this kind of context is likely this: When you're present, getting into stuff is not safe. He might get in trouble. So, whether or not you think he "knows" is neither here nor there. "Getting it" is irrelevant.
When you're not around, it works to get into food and things because nothing bad happens. It's perfectly safe, there's a big pay-off and besides that, it works great! That's as far as it goes in his mind.
When ever there is an unworkable consequence to this behavior, you happen to be near by, watching. Cause and effect. You're present, the effect is that it doesn't work. You're not present, the behavior works. Dogs' whole world is cause and effect. You'd have to have the consequence for this behavior happen in both scenarios...when you're not in the room and when you are. You'd have to convince him that it is dangerous to engage in that behavior regardless of what else is going on, regardless if you're in the room. (motion detector things that poof out air or a siren) Of course, he may habituate to those things in time. Or you'd have to reward him for leaving things alone, first when you're present, then when you're absent, but just for a second, gradually increasing the duration. And you'd have to prevent him from getting reinforced by grabbing the thing you want him to leave. You'd have to reward him for leaving it with something much better than the thing he was wanting to get. And it would have to happen every time.
It can be done, but imo, it's not generally that hard to simply keep things put away and don't leave anything out ever...that he might like. Some dogs are easier to work this out with than others. My son's dog is quite good about leaving things alone on the coffee table when no one is in the room. Once your guy does it and succeeds, he finds out it works great...."woo hoo, I scored," the behavior will repeat. It's not that he's immoral or "knows but is just being naughty." It's the way dogs are. Period. They're hard wired to scavange and be opportunists and they don't have the same value system or morals that we have.
With the digging in the garden, you can't leave him unsupervised. But when you are around, you can give him a sand box with special toys and some biscuits buried in there. Let him be reinforced for digging there or in a designated area in your yard. PREVENT and/or distract him away from digging anywhere he pleases.
With anything.....prevention in the first place so he's not reinforced for that behavior is numero uno for importance. Short of that: interception if the behavior is already begun, distraction, giving an alternative, reinforcing (really good treat or toy, whatever he loves) for engaging in the behavior you want.