The lemmy ‘community’ table has a boolean property called “hidden”. If you flip that to true it will hide the communities but they will still be available to anyone who subscribes to them.
They will not appear in the community list, nor the search results, nor public feeds. Users wanting to subscribe to them can simply go to ‘instance.domain/c/name@remote.instance’ and then click subscribe and they will get all updates and see those communities in their feeds.
Example queries.
Specific community:
UPDATE community SET hidden = 't' WHERE name = 'communityname' AND actor_id LIKE 'https://instance.domain%';
All communities from a specific instance:
UPDATE community SET hidden = 't' WHERE actor_id LIKE 'https://instance.domain%';
This latter one will have to be rerun periodically or when a new entry is added to the table.
You can also hide remote communities that are NSFW like this:
UPDATE community SET hidden = 't' WHERE nsfw = 't' AND actor_id LIKE 'https://instance.domain%';
EDIT:
In order to run these queries and explore the database a bit you can enter the postgres container with this command:
docker exec -it instancedomain_postgres_1 busybox /bin/sh
and then run psql -U databaseuser
(default user is lemmy) to acces the interactive postgres interface.