28

I have a server with Hangfire installed. I haven't checked it for a while and it seems one recurring job has gone rogue. It stopped working and then it has stacked up with retries resulting in a complete lock it seems.

I would like to clear my database and start over. Can i just delete the data from all tables or should I drop the tables and let Hangfire recreate them for me? Am I risking anything by doing this?

Hangfire.State using entire Azure Basic database 2GB of space. enter image description here

Ogglas
  • 62,132
  • 37
  • 328
  • 418

6 Answers6

37

I ended up dropping the tables, at first the query did not work at all, it just kept going and nothing happened. I then used TRUNCATE TABLE [HangFire].[State] and it all worked like a charm after. Here is the script I used for Hangfire 1.5.6 with UseSqlServerStorage:

GO
PRINT N'Dropping [HangFire].[FK_HangFire_State_Job]...';


GO
ALTER TABLE [HangFire].[State] DROP CONSTRAINT [FK_HangFire_State_Job];


GO
PRINT N'Dropping [HangFire].[FK_HangFire_JobParameter_Job]...';


GO
ALTER TABLE [HangFire].[JobParameter] DROP CONSTRAINT [FK_HangFire_JobParameter_Job];


GO
PRINT N'Dropping [HangFire].[Schema]...';


GO
DROP TABLE [HangFire].[Schema];


GO
PRINT N'Dropping [HangFire].[Job]...';


GO
DROP TABLE [HangFire].[Job];


GO
PRINT N'Dropping [HangFire].[State]...';


GO
DROP TABLE [HangFire].[State];


GO
PRINT N'Dropping [HangFire].[JobParameter]...';


GO
DROP TABLE [HangFire].[JobParameter];


GO
PRINT N'Dropping [HangFire].[JobQueue]...';


GO
DROP TABLE [HangFire].[JobQueue];


GO
PRINT N'Dropping [HangFire].[Server]...';


GO
DROP TABLE [HangFire].[Server];


GO
PRINT N'Dropping [HangFire].[List]...';


GO
DROP TABLE [HangFire].[List];


GO
PRINT N'Dropping [HangFire].[Set]...';


GO
DROP TABLE [HangFire].[Set];


GO
PRINT N'Dropping [HangFire].[Counter]...';


GO
DROP TABLE [HangFire].[Counter];


GO
PRINT N'Dropping [HangFire].[Hash]...';


GO
DROP TABLE [HangFire].[Hash];


GO
PRINT N'Dropping [HangFire].[AggregatedCounter]...';


GO
DROP TABLE [HangFire].[AggregatedCounter];


GO
PRINT N'Dropping [HangFire]...';


GO
DROP SCHEMA [HangFire];


GO
PRINT N'Update complete.';


GO
Ogglas
  • 62,132
  • 37
  • 328
  • 418
  • 4
    But don't forget to add the foreign keys again after dropping them. By the way, it's better to update to the latest [1.6.5](https://www.nuget.org/packages/Hangfire/), there were a lot of fixes since your versions for both Hangfire.Core and Hangfire.SqlServer to not to face with problems like this. – odinserj Oct 11 '16 at 15:24
31

I've seen a pretty interesting solution here

TRUNCATE TABLE [HangFire].[AggregatedCounter]
TRUNCATE TABLE [HangFire].[Counter]
TRUNCATE TABLE [HangFire].[JobParameter]
TRUNCATE TABLE [HangFire].[JobQueue]
TRUNCATE TABLE [HangFire].[List]
TRUNCATE TABLE [HangFire].[State]
DELETE FROM [HangFire].[Job]
DBCC CHECKIDENT ('[HangFire].[Job]', reseed, 0)
UPDATE [HangFire].[Hash] SET Value = 1 WHERE Field = 'LastJobId'

This basically truncates all the tables and resets the seeding for the Job table (reseting job id's)

I think it's also worth mentioning that it is good and sensible practice to stop your application and make sure the worker processes have cleared up on your server before running any scripts

JosePaya
  • 323
  • 3
  • 7
10

You can clear all hangfire jobs by Truncating all the hangfire tables and reset the seeding for the Job table. (Resetting job id's)

TRUNCATE TABLE [HangFire].[AggregatedCounter]
TRUNCATE TABLE [HangFire].[Counter]
TRUNCATE TABLE [HangFire].[JobParameter]
TRUNCATE TABLE [HangFire].[JobQueue]
TRUNCATE TABLE [HangFire].[List]
TRUNCATE TABLE [HangFire].[State]
DELETE FROM [HangFire].[Job]
DBCC CHECKIDENT ('[HangFire].[Job]', reseed, 0)
UPDATE [HangFire].[Hash] SET Value = 1 WHERE Field = 'LastJobId'

I found this solution on Hangfire Database Cleanup

Bibin Gangadharan
  • 1,393
  • 12
  • 13
3

For Hangfire Postgresql you can run the following commands. In order for the tables to be rebuilt, you must restart the application

ALTER TABLE hangFire.state DROP CONSTRAINT state_jobid_fkey;

ALTER TABLE hangFire.jobParameter DROP CONSTRAINT jobparameter_jobid_fkey;

DROP TABLE hangFire.Schema;

DROP TABLE hangFire.Job;

DROP TABLE hangFire.State;

DROP TABLE hangFire.jobParameter;

DROP TABLE hangFire.jobQueue;

DROP TABLE hangFire.Server;

DROP TABLE hangFire.list;

DROP TABLE hangFire.set;
DROP TABLE hangFire.lock;

DROP TABLE hangFire.counter;

DROP TABLE hangFire.hash;

DROP SCHEMA hangFire Cascade;
Sreilus
  • 131
  • 2
  • 4
2

Put this in your Startup.cs class before Hangfire Configuration. It will clean all the tables automatically before application starts.

            DatabaseEntities db = new DatabaseEntities();   //EntityFrameWork Class

            string sqlCommand = "" +
            "DELETE FROM[HangFire].[AggregatedCounter];" +
            "DELETE FROM[HangFire].[Counter];" +
            "DELETE FROM[HangFire].[JobParameter];" +
            "DELETE FROM[HangFire].[JobQueue];" +
            "DELETE FROM[HangFire].[List];" +
            "DELETE FROM[HangFire].[State];" +
            "DELETE FROM[HangFire].[Job];" +
            "DELETE FROM[HangFire].[Server];" +
            "DELETE FROM[HangFire].[Schema];" +
            "DELETE FROM[HangFire].[Set];" +
            "DELETE FROM[HangFire].[Hash];";

            var rowsDeleted = db.Database.ExecuteSqlCommand(sqlCommand);
Md Shahriar
  • 2,072
  • 22
  • 11
  • Damn, this works 100%? So you can easily programatically reset Hangfire?? If you run on N appservers, must all appservers first to be stopped? – sabiland Jan 06 '23 at 06:57
0

Oracle version:

TRUNCATE TABLE HANGFIRE.HF_AGGREGATED_COUNTER;
TRUNCATE TABLE HANGFIRE.HF_Counter;
TRUNCATE TABLE HANGFIRE.HF_Job_Parameter;
TRUNCATE TABLE HANGFIRE.HF_Job_Queue;
TRUNCATE TABLE HANGFIRE.HF_List;
TRUNCATE TABLE HANGFIRE.HF_JOB_State;
TRUNCATE TABLE HANGFIRE.HF_hash;
TRUNCATE TABLE HangFire.HF_set;
TRUNCATE TABLE HangFire.HF_server;
DELETE FROM HANGFIRE.HF_Job;
majjam
  • 1,286
  • 2
  • 15
  • 32