Some of our simple products were not correctly assigned to a configurable product. And, some of them were visible independently in the front end. My task was to assign simple products to correct configurable product and make associated products to not visible individually. It was easy to find configurable product and associated simple products because of the SKU they had. All simple products had sku configurableSKU__*.
So after some trials this code worked for me.