Deleting DB tables don't work with unwatchContractEvent

Here is my code I am using to delete the event sync and the associated tables, It is showing me error even when the table exists.

const unwatchChannels = async () => {
  // code example of creating a sync event from cloud code
  await Moralis.start({ serverUrl, appId, masterKey });
  let options = { tableName: "channels" };
  Moralis.Cloud.run("unwatchContractEvent", options, { useMasterKey: true }).then(
    (result: any) => {
      console.log(result);
    }
  );
};

The error

{
  success: false,
  error: 'No event found with tableName "channels" that is created via cloud-code'
}

I used Moralis admin for contract sync.

can you delete it from the admin interface?

Yes, Thats what i have been doing and its really a tedious task.
It is a cumbersome task to delete all these contracts sync again and again while doing development.

try this one: coreservices_unwatchContractEvent

Error I got


/Users/saurav/Programs/ama.fans/mvp-contracts/node_modules/moralis-v1/lib/node/RESTController.js:423
        error = new _ParseError.default(errorJSON.code, errorJSON.error);
                ^
ParseError: Invalid function: "coreservices_unwatchContractEvent"
    at handleError (/Users/saurav/Programs/ama.fans/mvp-contracts/node_modules/moralis-v1/lib/node/RESTController.js:423:17)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 141
}
1 Like

Thanks a lot, It worked for me too and deleted all the events.
Is there a way, I can delete the Tables in the database too, even if they have entries ?

I mean, Delete all the sync events and their tables in the mongodb.

Thanks @cryptokid ,

I have facing same error but now it’s working fine using ‘coreservices_removeEventSync’.

Please update below document as well , so other people’s not get this error.

https://v1docs.moralis.io/moralis-dapp/automatic-transaction-sync/smart-contract-events#unwatch-existing-smart-contract-event

The tables gets flushed but I have to manually remove all the tables one by one.
I am using this code to delete all the tables, however, This drop all the documents in all the tables but doesn’t remove the table itself.

const deleteTables = () => {
  const uri  = `mongodb://${moralis_ip}:56728/parse`
  console.log(uri, {useNewUrlParser: true})
  mongoose.connect(uri);
  const connection = mongoose.connection;
  connection.once("open", function() {
    console.log("MongoDB connected successfully");
    connection.db.listCollections().toArray(function(err: any, names: any) {
        if (err) {
            console.log(err);
        } else {
          for (var i: number = 0; i < names.length; i++) {
            if (!names[i].name.startsWith("_")){
              let collection_name = names[i].name
              mongoose.connection.db.dropCollection(
                collection_name,
                function(err: any, result: any) {
                    console.log(collection_name + " collection dropped");
                }
            );
            }
          }
        }
      }
      )})
      return ;

}

if you are connecting directly to mongo db like that, you should also find a way to delete the table, search for something related to Schemas in the database