Clear\delete all workflows from database

Hi, I use a little program nodejs that suppose to import all my workflows (I succeeded in doing that), but I want to clear all the database before the import how can I do that using the cli ?

Hey @Asaf_Shay,

We don’t have a CLI option to delete all workflows but I did have a quick think about it though and came up with the workflow below which will delete all other workflows. This uses the n8n api through the n8n node to first get all the workflows then if the workflow ID is not the ID of the workflow currently running it is deleted.

If you do run this make sure you have a backup first as it will delete things, I have also disabled the delete node so you can run it as it is and see what would get deleted.

This does not delete credentials.

i saw that i can call n8n api like this:

const axios = require(‘axios’);

// Set the n8n API URL and authentication credentials
const apiUrl = 'http://localhost:5678/rest';
const auth = {
  username: 'n8n',
  password: 'n8n'
};

// Define the request configuration to get all workflows
const getAllWorkflowsConfig = {
  method: 'get',
  url: `${apiUrl}/workflows`,
  auth: auth,
};

// Send the get all workflows request
axios(getAllWorkflowsConfig)
  .then(function (response) {
	const workflows = response.data.data;
	console.log(`Found ${workflows.length} workflows`);
	// Loop through all workflows and delete each one
	workflows.forEach((workflow) => {
	  const deleteWorkflowConfig = {
		method: 'delete',
		url: `${apiUrl}/workflow/${workflow.id}`,
		auth: auth,
	  };
	  axios(deleteWorkflowConfig)
		.then(function (response) {
		  console.log(`Workflow ${workflow.id} deleted successfully`);
		})
		.catch(function (error) {
		  console.log(`Error deleting workflow ${workflow.id}: ${error.message}`);
		});
	});
  })
  .catch(function (error) {
	console.log(`Error getting workflows: ${error.message}`);
	});

the problem is that I get 401 , I guessed that the username and password, Probably my mistake there, what should the values ​​be?

even if I set
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=n8n
N8N_BASIC_AUTH_PASSWORD=n8n

I still getting 401

anyone please ?

Why don’t you delete the workflows like Jon suggested?

If you are running into problems with authenticating to the API, and you are running the self-hosted version of n8n with docker and basic auth enabled, then maybe this thread has a solution.

does n8n have the option to remove one workflow using the cli ?

image

Not that I am aware of which led me to make the workflow that uses our api to handle it. You could manually use our public api as well which is similar to what you have attempted but using the api we document.

the problem with the api is that I need the n8n to be running, and I prefer the cli because of that

If n8n isn’t running then the workflow or api won’t do the job as you say because it needs n8n to be running.

The other option would be to query the database yourself and delete the records or just delete the database and make a new one depending on if the rest of the data is needed.

I have to say though looking at your post history you have the strangest set up I have seen. If you add the credentials as well through the cli docker would be very useful for it.

how can I query the DB?
just to make clear, when machine is turn on the windows scheduler starting my nodejs program that download workflows, I want that the program clear all workflows before import them

Hey @Asaf_Shay,

If you are using the sqlite database you would need to use an application that can read it but it would need to be before your node program starts.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.